Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope with arity 0 and enum requests compatible #62

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1831e6d
Update patch version for ruby
ferdinandrosario Jun 29, 2015
4a217cc
Scope with arity 0 and enum requests compatible
hachpai Jul 15, 2015
ad3ee13
Merge pull request #59 from ferdinandrosario/ferdinandrosario-patch-1
jhund Dec 1, 2015
adf5fe2
update patch and readme
ferdinandrosario Dec 1, 2015
f2196f7
Merge pull request #72 from ferdinandrosario/upgrade
jhund Dec 1, 2015
730e4b9
Turbolinks 5 compatibility
olegantonyan Apr 12, 2016
5965ffc
prevent double initialization
olegantonyan Apr 12, 2016
286d3ce
spelling
olegantonyan Apr 12, 2016
08bd20e
fix double init
olegantonyan Apr 12, 2016
968d95f
Don't parse a hypen (-) as an int, also allow 0
geraintwhite May 9, 2016
f27f173
Change array join to safe_join
ScottKolo Jun 24, 2016
ee98561
fix stringify_keys! on params deprication warning in rails 5
olegantonyan Aug 22, 2016
3ddcee1
Add Ruby 2.3.1 into Travis CI
Oct 15, 2016
d1d533c
Merge pull request #87 from olegantonyan/turbolinks5
jhund Nov 7, 2016
a5ae860
Merge pull request #109 from rodrigoargumedo/patch-1
jhund Nov 7, 2016
835f5ac
Merge pull request #101 from olegantonyan/rails5_deprication
jhund Nov 7, 2016
aa112fa
Merge pull request #96 from ScottKolo/master
jhund Nov 7, 2016
a1f2583
Merge pull request #88 from grit96/patch-1
jhund Nov 7, 2016
25860a1
Updated to Ruby 2.2.2 in CI test matrix
jhund Nov 7, 2016
ad02e47
Removed unused l_var and simplified code
jhund Nov 7, 2016
73c5ad6
Add to assets.precompile only prior to Rails 5
jhund Nov 7, 2016
58e1376
Name spaced test code in specs to silence warnings
jhund Nov 7, 2016
4af0c91
Added asset initializer to make filterrific-spinner available under R…
jhund Nov 7, 2016
e32867a
Merged conflict
jhund Nov 7, 2016
3424bb5
Cosmetic changes, documentation updates
jhund Nov 7, 2016
b520aaa
Refactored and improved regex for casting string params to integer
jhund Nov 7, 2016
74b0b47
Updated changelog
jhund Nov 7, 2016
0c18426
Bump to 2.1.0
jhund Nov 7, 2016
8cacc06
Tweaked CI test matrix to test older Ruby versions only up to Rails 4.2
jhund Nov 7, 2016
d2c2810
Further tweaked CI test matrix
jhund Nov 7, 2016
b03c3fb
Changed how Gem dependencies are handled for the various Rails versio…
jhund Nov 7, 2016
78ec0e9
Removed testing for Ruby 1.9.3. It should still work, however we’re n…
jhund Nov 7, 2016
6507aa6
Updated readme, cosmetic code change
jhund Nov 11, 2016
8e2c176
Reverted asset initialization back to working state for versions prio…
jhund Nov 11, 2016
bb5953b
Updated Changelog
jhund Nov 11, 2016
4458c05
Bump to 2.1.1
jhund Nov 11, 2016
ec3d308
Removed older way of initializing assets, relying on config/initializ…
jhund Nov 11, 2016
d48c91b
Bump to 2.1.2
jhund Nov 11, 2016
5564769
Updated Readme, dropped support and testing for Rails 3.x
jhund Nov 12, 2016
41ad41f
v2
hachpai Jan 30, 2017
78fea5e
v2
hachpai Jan 30, 2017
49af5fc
v2.1
hachpai Jan 30, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ language: ruby
script: "bundle exec rake test"

rvm:
- 1.9.3
- 2.0.0
- 2.1.2
- 2.2.0
- 2.1.7
- 2.2.3
- 2.3.1
gemfile:
- Gemfile-Rails-4.x
- Gemfile
matrix:
exclude:
- rvm: 2.0.0
gemfile: Gemfile
- rvm: 2.1.7
gemfile: Gemfile
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
### 2.1.2

* Removed older way of initializing assets, relying on config/initializers/assets.rb for all versions of Rails.

### 2.1.1

* Reverted asset initialization back to working state for versions prior to Rails5.

## 2.1.0

* Updated filterrific for Rails 5 compatibility:
* Turbolinks compatibility (thanks @olegantonyan)
* Updated to Ruby 2.2.2 in CI test matrix
* Added Ruby 2.3.1 to CI test matrix (thanks @rodrigoargumedo)
* Fixed #stringify_keys deprecation warning (thanks @olegantonyan)
* When generating sorting headers, allow usage of HTML (e.g., for icons) using #safe_join (thanks @ScottKolo)
* Improved param casting to Int: hyphens and zeros (thanks @grit96)


### 2.0.5

* Feature: Allow disabling of session persistence by passing `false` as
Expand Down
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
source "https://rubygems.org"

gemspec
gem 'rails'

group :development, :test do
gem 'gem-release'
gem 'wwtd' # to simulate travis locally
end
8 changes: 8 additions & 0 deletions Gemfile-Rails-4.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem 'rails', '~> 4.2'

group :development, :test do
gem 'gem-release'
gem 'wwtd' # to simulate travis locally
end
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@ Every commit to Filterrific is automatically tested against the following scenar

| Rails version | Ruby environments | Database adapters | Build status |
|---------------|--------------------------------|------------------------------------|--------------|
| Rails 4.2 | MRI 1.9.3, 2.0.0, 2.1.2, 2.2.0 | mysql, mysql2, postgresql, sqlite3 |[![Build Status](https://travis-ci.org/jhund/filterrific_demo.svg?branch=rails-4.2)](https://travis-ci.org/jhund/filterrific_demo)|
| Rails 4.1 | MRI 1.9.3, 2.0.0, 2.1.2, 2.2.0 | mysql, mysql2, postgresql, sqlite3 |[![Build Status](https://travis-ci.org/jhund/filterrific_demo.svg?branch=rails-4.1)](https://travis-ci.org/jhund/filterrific_demo)|
| Rails 4.0 | MRI 1.9.3, 2.0.0, 2.1.2, 2.2.0 | mysql, mysql2, postgresql, sqlite3 |[![Build Status](https://travis-ci.org/jhund/filterrific_demo.svg?branch=rails-4.0)](https://travis-ci.org/jhund/filterrific_demo)|
| Rails 3.2 | MRI 1.9.3, 2.0.0, 2.1.2 | mysql, mysql2, postgresql, sqlite3 |[![Build Status](https://travis-ci.org/jhund/filterrific_demo.svg?branch=rails-3.2)](https://travis-ci.org/jhund/filterrific_demo)|
| Rails 5.x | MRI 2.0.0, 2.1.7, 2.2.3, 2.3.1 | mysql2, postgresql |[![Build Status](https://travis-ci.org/jhund/filterrific_demo.svg?branch=rails-5.x)](https://travis-ci.org/jhund/filterrific_demo)|
| Rails 4.x | MRI 2.0.0, 2.1.7, 2.2.3, 2.3.1 | mysql2, postgresql |[![Build Status](https://travis-ci.org/jhund/filterrific_demo.svg?branch=rails-4.x)](https://travis-ci.org/jhund/filterrific_demo)|

Filterrific version 1.x should work on older versions of Rails and Ruby, however
the 1.x branch is not supported any more.
Filterrific up to version 2.1.x should work on Rails 3.2 and Ruby 1.9.3. I stopped testing it though as it became too cumbersome to manage gem dependencies.

Filterrific version 1.x should work on versions prior to Rails 3.2 and older Rubies, however the 1.x branch is not supported any more.

### Guidelines for submitting issues

Please post questions related to usage at [StackOverflow](http://stackoverflow.com/questions/tagged/filterrific) under the `filterrific` tag.

If you think you've found a bug, or have a feature request, then create an issue here on Github. You'll make my job easier if you follow these guidelines:

* Please keep in mind that I do this in my spare time. I appreciate it if you first do everything you can on your own: read the detailed filterrific documentation, look for similar issues on StackOverflow, search the internets, etc.
* Please keep in mind that I do this in my spare time. To you this software is free as in `beer`, to me it's free as in `baby`. I appreciate it if you first do everything you can on your own: read the detailed [Filterrific documentation](http://filterrific.clearcove.ca), look for similar issues on [StackOverflow](http://stackoverflow.com/questions/tagged/filterrific), search the internets, etc.
* If you're stuck, give me sufficient context so that I have a chance to identify the issue:
* what version of filterrific are you using? (look in your `Gemfile.lock`)
* what version of Rails are you using? (look in your `Gemfile.lock`)
Expand Down Expand Up @@ -75,4 +73,4 @@ If you think you've found a bug, or have a feature request, then create an issue

### Copyright

Copyright (c) 2010 - 2015 Jo Hund. See [(MIT) LICENSE](https://github.com/jhund/filterrific/blob/master/MIT-LICENSE) for details.
Copyright (c) 2010 - 2016 Jo Hund. See [(MIT) LICENSE](https://github.com/jhund/filterrific/blob/master/MIT-LICENSE) for details.
17 changes: 14 additions & 3 deletions app/assets/javascripts/filterrific/filterrific-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ Filterrific.submitFilterForm = function(){
})(jQuery);



// Initialize event observers on document ready and turbolinks page:load
jQuery(document).on('ready page:load', function() {
Filterrific.init = function() {
// Add change event handler to all Filterrific filter inputs.
$('#filterrific_filter').on(
"change",
Expand All @@ -94,4 +92,17 @@ jQuery(document).on('ready page:load', function() {
0.5,
Filterrific.submitFilterForm
);
};


// Initialize event observers on document ready and turbolinks page:load
jQuery(document).on('turbolinks:load', function() {
// Prevent double initilisation. With turbolinks 5 this function
// will be called twice: on 'ready' and 'turbolinks:load'
jQuery(document).off('ready page:load')
Filterrific.init();
});

jQuery(document).on('ready page:load', function() {
Filterrific.init();
});
1 change: 1 addition & 0 deletions config/initializers/assets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rails.application.config.assets.precompile += %w(filterrific/filterrific-spinner.gif)
7 changes: 0 additions & 7 deletions filterrific.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,4 @@ Gem::Specification.new do |gem|
'README*',
'{app,bin,doc,lib,spec}/**/*'
]

gem.add_dependency 'rails', ['>= 3.1.0']

gem.add_development_dependency 'bundler', ['>= 1.6.1']
gem.add_development_dependency 'gem-release', ['>= 0.7.3']
gem.add_development_dependency 'rake', ['>= 10.3.2']
gem.add_development_dependency 'wwtd', ['>= 0.5.5'] # to simulate travis locally
end
20 changes: 8 additions & 12 deletions lib/filterrific/action_view_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,18 @@ def filterrific_sorting_link(filterrific, sort_key, opts = {})
# @return [String] an HTML fragment
def filterrific_sorting_link_reverse_order(filterrific, new_sort_key, opts)
# current sort column, toggle search_direction
new_sort_direction, current_sort_direction_indicator = if 'asc' == opts[:current_sort_direction]
['desc', opts[:ascending_indicator]]
else
['asc', opts[:descending_indicator]]
end
new_sorting = [new_sort_key, new_sort_direction].join('_')
css_classes = [
new_sort_direction = 'asc' == opts[:current_sort_direction] ? 'desc' : 'asc'
new_sorting = safe_join([new_sort_key, new_sort_direction], '_')
css_classes = safe_join([
opts[:active_column_class],
opts[:html_attrs].delete(:class)
].compact.join(' ')
].compact, ' ')
new_filterrific_params = filterrific.to_hash
.with_indifferent_access
.merge(opts[:sorting_scope_name] => new_sorting)
url_for_attrs = opts[:url_for_attrs].merge(:filterrific => new_filterrific_params)
link_to(
[opts[:label], opts[:current_sort_direction_indicator]].join(' '),
safe_join([opts[:label], opts[:current_sort_direction_indicator]], ' '),
url_for(url_for_attrs),
opts[:html_attrs].reverse_merge(:class => css_classes, :method => :get, :remote => true)
)
Expand All @@ -129,11 +125,11 @@ def filterrific_sorting_link_reverse_order(filterrific, new_sort_key, opts)
# @return [String] an HTML fragment
def filterrific_sorting_link_new_column(filterrific, new_sort_key, opts)
new_sort_direction = opts[:default_sort_direction]
new_sorting = [new_sort_key, new_sort_direction].join('_')
css_classes = [
new_sorting = safe_join([new_sort_key, new_sort_direction], '_')
css_classes = safe_join([
opts[:inactive_column_class],
opts[:html_attrs].delete(:class)
].compact.join(' ')
].compact, ' ')
new_filterrific_params = filterrific.to_hash
.with_indifferent_access
.merge(opts[:sorting_scope_name] => new_sorting)
Expand Down
7 changes: 5 additions & 2 deletions lib/filterrific/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ def filterrific_find(filterrific_param_set)
filterrific_available_filters.each do |filter_name|
filter_param = filterrific_param_set.send(filter_name)
next if filter_param.blank? # skip blank filter_params
ar_rel = ar_rel.send(filter_name, filter_param)
begin
ar_rel = ar_rel.send(filter_name, filter_param)
rescue ArgumentError #if we have a scope with arity 0 or enum query, we can perform the request without the parameter
ar_rel = ar_rel.send(filter_name) if (filter_param == 1)
end
end

ar_rel
end

Expand Down
4 changes: 0 additions & 4 deletions lib/filterrific/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,5 @@ class Engine < ::Rails::Engine
extend Filterrific::ActiveRecordExtension
end

initializer "filterrific" do |app|
app.config.assets.precompile += %w(filterrific-spinner.gif)
end

end
end
15 changes: 12 additions & 3 deletions lib/filterrific/param_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def initialize(a_model_class, filterrific_params = {})
# Persistence, baby. By the time you submit changes to one filter, all the others
# will be already initialized with the defaults.
filterrific_params = model_class.filterrific_default_filter_params if filterrific_params.blank?
filterrific_params.stringify_keys!
if defined?(ActionController::Parameters) && filterrific_params.is_a?(ActionController::Parameters)
filterrific_params = filterrific_params.permit(model_class.filterrific_available_filters).to_h.stringify_keys
else
filterrific_params.stringify_keys!
end
filterrific_params = condition_filterrific_params(filterrific_params)
define_and_assign_attr_accessors_for_each_filter(filterrific_params)
end
Expand Down Expand Up @@ -82,19 +86,24 @@ def condition_filterrific_params(fp)
fp[key] = val.call
when val.is_a?(Array)
# type cast integers in the array
fp[key] = fp[key].map { |e| e =~ /\A[1-9\-]\d*\z/ ? e.to_i : e }
fp[key] = fp[key].map { |e| e =~ integer_detector_regex ? e.to_i : e }
when val.is_a?(Hash)
# type cast Hash to OpenStruct so that nested params render correctly
# in the form
fp[key] = OpenStruct.new(fp[key])
when val =~ /\A[1-9\-]\d*\z/
when val =~ integer_detector_regex
# type cast integer
fp[key] = fp[key].to_i
end
end
fp
end

# Regex to detect if str represents and int
def integer_detector_regex
/\A-?([1-9]\d*|0)\z/
end

# Defines attr accessors for each available_filter on self and assigns
# values based on fp.
# @param fp [Hash] filterrific_params with stringified keys
Expand Down
2 changes: 1 addition & 1 deletion lib/filterrific/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

module Filterrific
VERSION = "2.0.5"
VERSION = "2.1.2"
end
36 changes: 18 additions & 18 deletions spec/filterrific/action_controller_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@

module Filterrific

class TestController
include ActionControllerExtension
def action_name; 'index'; end
def controller_name; 'test_controller'; end
def session
{
'test_controller#index' => {
'filter1' => '1_from_session',
'filter2' => '2_from_session',
describe ActionControllerExtension do

class TestController
include ActionControllerExtension
def action_name; 'index'; end
def controller_name; 'test_controller'; end
def session
{
'test_controller#index' => {
'filter1' => '1_from_session',
'filter2' => '2_from_session',
}
}
}
end
end
end

class TestModelClass
def self.filterrific_available_filters; %w[filter1 filter2]; end
def self.filterrific_default_filter_params
{ 'filter1' => '1_from_model_defaults' }
class TestModelClass
def self.filterrific_available_filters; %w[filter1 filter2]; end
def self.filterrific_default_filter_params
{ 'filter1' => '1_from_model_defaults' }
end
end
end

describe ActionControllerExtension do

describe '#initialize_filterrific' do

Expand Down
8 changes: 4 additions & 4 deletions spec/filterrific/action_view_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

module Filterrific

class ViewContext
include ActionViewExtension
end

describe ActionViewExtension do

class ViewContext
include ActionViewExtension
end

it 'responds to #form_for_filterrific' do
ViewContext.new.must_respond_to(:form_for_filterrific)
end
Expand Down
28 changes: 14 additions & 14 deletions spec/filterrific/active_record_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@

module Filterrific

# Container for test data
class TestData
describe ActiveRecordExtension do

def self.filterrific_available_filters
%w[search_query sorted_by with_country_id]
end
# Container for test data
class TestDataARES

def self.filterrific_default_filter_params
{ 'sorted_by' => 'name_asc' }
end
def self.filterrific_available_filters
%w[search_query sorted_by with_country_id]
end

end
def self.filterrific_default_filter_params
{ 'sorted_by' => 'name_asc' }
end

describe ActiveRecordExtension do
end

let(:filterrific_class){
Class.new(ActiveRecord::Base) do
filterrific(
available_filters: TestData.filterrific_available_filters,
default_filter_params: TestData.filterrific_default_filter_params
available_filters: TestDataARES.filterrific_available_filters,
default_filter_params: TestDataARES.filterrific_default_filter_params
)
end
}
Expand All @@ -46,13 +46,13 @@ def self.filterrific_default_filter_params

it "initializes filterrific_available_filters" do
filterrific_class.filterrific_available_filters.must_equal(
TestData.filterrific_available_filters
TestDataARES.filterrific_available_filters
)
end

it "initializes filterrific_default_filter_params" do
filterrific_class.filterrific_default_filter_params.must_equal(
TestData.filterrific_default_filter_params
TestDataARES.filterrific_default_filter_params
)
end

Expand Down
Loading