filter_factory 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,6 +25,11 @@ Describe your filter:
25
25
  field :title, :regex
26
26
  field :author, :eq
27
27
  field :views, :gte
28
+
29
+ # add aliases for the same field with different conditions
30
+ field :created_at, :gte, alias: :created_at_gte
31
+ field :created_at, :lte, alias: :created_at_lte
32
+
28
33
  # ...
29
34
  end
30
35
 
@@ -46,6 +51,16 @@ Render form as you want in your view:
46
51
  <br/>
47
52
  <%= f.number_field :views %>
48
53
  </div>
54
+ <div>
55
+ <%= f.label :created_at_gte %>
56
+ <br/>
57
+ <%= f.date_select :created_at_gte %>
58
+ </div>
59
+ <div>
60
+ <%= f.label :created_at_lte %>
61
+ <br/>
62
+ <%= f.date_select :created_at_lte %>
63
+ </div>
49
64
  <div class="actions">
50
65
  <%= f.submit "Filter" %>
51
66
  </div>
@@ -1,10 +1,12 @@
1
1
  module FilterFactory
2
2
  class Field
3
- attr_reader :name, :condition, :options
3
+ attr_reader :name, :condition, :alias, :options
4
4
  attr_accessor :value
5
5
 
6
6
  def initialize(name, condition, options = {})
7
- @name, @condition, @options = name, condition, options
7
+ valid_options = [:alias]
8
+ @name, @condition, @options = name, condition, options.reject{|k,v| !valid_options.include?(k)}
9
+ @alias = @options[:alias] || @name
8
10
  end
9
11
  end
10
12
  end
@@ -13,7 +13,7 @@ module FilterFactory
13
13
 
14
14
  def attributes
15
15
  @fields.inject(HashWithIndifferentAccess.new) do |acc,field|
16
- acc[field.name] = field.value
16
+ acc[field.alias] = field.value
17
17
  acc
18
18
  end
19
19
  end
@@ -34,10 +34,10 @@ module FilterFactory
34
34
  end
35
35
 
36
36
  private
37
- def field(name, condition)
38
- Field.new(name, condition).tap do |field|
39
- define_singleton_method(name){ field.value }
40
- define_singleton_method("#{name}="){|val| field.value = val }
37
+ def field(name, condition, options={})
38
+ Field.new(name, condition, options).tap do |field|
39
+ define_singleton_method(field.alias){ field.value }
40
+ define_singleton_method("#{field.alias}="){|val| field.value = val }
41
41
 
42
42
  @fields << field
43
43
  end
@@ -6,9 +6,9 @@ module FilterFactory
6
6
  FilterFactory::Mongoid::Condition.new(field.name, field.value).method(field.condition)
7
7
  end
8
8
 
9
- conditions.inject(nil) do |res,condition|
10
- res ? res.instance_eval(&condition) : instance_eval(&condition)
11
- end || self
9
+ conditions.inject(self) do |res,condition|
10
+ res = res.instance_eval(&condition)
11
+ end
12
12
  end
13
13
  end
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module FilterFactory
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -12,14 +12,14 @@ describe ARPost do
12
12
  it "should execute filter methods chain" do
13
13
  filter = FilterFactory.create do
14
14
  field :title, :eq
15
- field :author, :eq
15
+ field :author, :eq, alias: :user
16
16
  field :views, :gte
17
17
  end
18
18
 
19
19
  sample = @posts.sample
20
20
 
21
21
  filter.title = sample.title
22
- filter.author = sample.author
22
+ filter.user = sample.author
23
23
 
24
24
  described_class.filter(filter).to_a.should == [sample]
25
25
  end
@@ -15,7 +15,7 @@ describe FilterFactory::Filter do
15
15
  filter.fields.map{|f| [f.name, f.condition]}.should == test_fields
16
16
  end
17
17
 
18
- it "should define singleton method for defined field" do
18
+ it "should define singleton method for defined field by its name if no alias option specified" do
19
19
  filter = described_class.create do
20
20
  field :name, :eq
21
21
  end
@@ -23,6 +23,14 @@ describe FilterFactory::Filter do
23
23
  filter.should respond_to(:name, :'name=')
24
24
  end
25
25
 
26
+ it "should define singleton method for defined field by its alias if alias option specified" do
27
+ filter = described_class.create do
28
+ field :name, :eq, alias: :my_name
29
+ end
30
+
31
+ filter.should respond_to(:my_name, :'my_name=')
32
+ end
33
+
26
34
  it "should get field value" do
27
35
  filter = described_class.create do
28
36
  field :name, :eq
@@ -52,20 +60,20 @@ describe FilterFactory::Filter do
52
60
  it "should return valid attributes" do
53
61
  filter = described_class.create do
54
62
  field :name, :eq
55
- field :surname, :regex
63
+ field :surname, :regex, alias: :last_name
56
64
  end
57
65
  filter.name = "test name"
58
66
 
59
- filter.attributes.should == HashWithIndifferentAccess.new({name: "test name", surname: nil})
67
+ filter.attributes.should == HashWithIndifferentAccess.new({name: "test name", last_name: nil})
60
68
  end
61
69
 
62
70
  it "should fill filter values from hash" do
63
71
  filter = described_class.create do
64
72
  field :name, :eq
65
- field :surname, :regex
73
+ field :surname, :regex, alias: :last_name
66
74
  end
67
75
 
68
- attributes = {name: "my test name", surname: "surname here"}
76
+ attributes = {name: "my test name", last_name: "surname here"}
69
77
  filter.attributes = attributes
70
78
  filter.attributes.should == HashWithIndifferentAccess.new(attributes)
71
79
  end
@@ -14,12 +14,12 @@ describe MPost do
14
14
 
15
15
  filter = FilterFactory.create do
16
16
  field :title, :eq
17
- field :author, :eq
17
+ field :author, :eq, alias: :user
18
18
  field :views, :gte
19
19
  end
20
20
 
21
21
  filter.title = sample.title
22
- filter.author = sample.author
22
+ filter.user = sample.author
23
23
 
24
24
  described_class.filter(filter).to_a.should == [sample]
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filter_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-05 00:00:00.000000000 Z
12
+ date: 2013-02-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Gem for easy ActiveRecord/Mongoid models filtering
15
15
  email:
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  requirements: []
61
61
  rubyforge_project:
62
- rubygems_version: 1.8.23
62
+ rubygems_version: 1.8.25
63
63
  signing_key:
64
64
  specification_version: 3
65
65
  summary: FilterFactory allows you to easily fetch ActiveRecord/Mongoid models that