0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
modify parts of a hash using a key_path
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 10.0.3

Runtime

 Project Readme

UrlPlumber

Gem Version Build Status

Ever had a table with multiple filter-options and the need to change one filter while keeping another active?

Using UrlPlumber you can change individual parts of your url parameters while not touching anything else.

Installation

Add this line to your application's Gemfile:

gem 'url_plumber'

And then execute:

$ bundle

Or install it yourself as:

$ gem install url_plumber

Usage

UrlPlumber has two main functions: changing values inside a hash and reading values stored inside of hash objects.

changing url parameters

The easiest way to get start is somewhere along these lines:

# url_plumber_helper.rb
module UrlPlumberHelper
  def plumb(key_path, value = nil)
    return url_for (@plumber ||= ::UrlPlumber::Plumber.new(params)).plumb(key_path => value)
  end
end

You can use it like this:

# some_view.haml
= link_to 'Clear Option A', plumb('filter.option_a')
= link_to 'Option A 1', plumb('filter.option_a', 1)
= link_to 'Option A 2', plumb('filter.option_a', 2)

= link_to 'Clear Option B', plumb('filter.option_b')
= link_to 'Option B 1', plumb('filter.option_b', 1)
= link_to 'Option B 2', plumb('filter.option_b', 2)

reading url parameters

# url_plumber_helper.rb
module UrlPlumberHelper
  def url_inquirer(key_path)
    return ::ActiveSupport::StringInquirer.new(@plumber ||= ::UrlPlumber::Plumber.new(params)).extract(key_path)
  end
end

You can use it like this:

# some_view.haml
- unless url_inquirer("filter.free_text").blank?
  = link_to 'clear search', root_path

Mix and match your plumber got you covert :)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request