filter_factory 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -33,7 +33,19 @@ Describe your filter:
33
33
  # ...
34
34
  end
35
35
 
36
- Render form as you want in your view:
36
+ Alternate way to create your filter:
37
+
38
+ @filter = FilterFactory.create do
39
+ # type filter type as method and field name as an argument
40
+ eq :author
41
+ gte :views
42
+
43
+ # supply options after field name
44
+ gte :created_at, alias: :created_at_gte
45
+ lte :created_at, alias: :created_at_lte
46
+ end
47
+
48
+ Render form as you want in your view (use aliases instead of field names if specified):
37
49
 
38
50
  <%= form_for @filter, as: :filter do |f| %>
39
51
  <div>
@@ -4,9 +4,18 @@ module FilterFactory
4
4
  attr_accessor :value
5
5
 
6
6
  def initialize(name, condition, options = {})
7
+ raise ArgumentError unless FilterFactory::Filter::CONDITIONS.include?(condition)
8
+
7
9
  valid_options = [:alias]
8
10
  @name, @condition, @options = name, condition, options.reject{|k,v| !valid_options.include?(k)}
9
11
  @alias = @options[:alias] || @name
10
12
  end
13
+
14
+ def ==(obj)
15
+ return false unless obj.is_a?(self.class)
16
+ [:name, :condition, :alias].inject(true) do |acc,attr|
17
+ acc && public_send(attr) == obj.public_send(attr)
18
+ end
19
+ end
11
20
  end
12
21
  end
@@ -7,6 +7,8 @@ module FilterFactory
7
7
 
8
8
  attr_reader :fields
9
9
 
10
+ CONDITIONS = [:eq, :ne, :lt, :lte, :gt, :gte, :all, :in, :nin, :regex, :exists, :presents]
11
+
10
12
  def initialize
11
13
  @fields = []
12
14
  end
@@ -29,13 +31,25 @@ module FilterFactory
29
31
  fields.select{|f| !f.value.nil? && f.value != ''}
30
32
  end
31
33
 
34
+ def get_field(name)
35
+ fields.find{|f| f.name == name}
36
+ end
37
+
32
38
  def persisted?
33
39
  false
34
40
  end
35
41
 
42
+ CONDITIONS.each do |condition|
43
+ define_method condition do |name,options={}|
44
+ field(name, condition, options)
45
+ end
46
+ end
47
+
36
48
  private
37
49
  def field(name, condition, options={})
38
50
  Field.new(name, condition, options).tap do |field|
51
+ raise DuplicateFieldError if fields.include?(field)
52
+
39
53
  define_singleton_method(field.alias){ field.value }
40
54
  define_singleton_method("#{field.alias}="){|val| field.value = val }
41
55
 
@@ -48,5 +62,7 @@ module FilterFactory
48
62
  new.tap{|filter| filter.instance_eval &block}
49
63
  end
50
64
  end
65
+
66
+ class DuplicateFieldError < StandardError; end
51
67
  end
52
68
  end
@@ -1,3 +1,3 @@
1
1
  module FilterFactory
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,60 @@
1
+ require "spec_helper"
2
+
3
+ describe FilterFactory::Field do
4
+ describe "#initialize" do
5
+ it "should create field with name & condition specified" do
6
+ field = described_class.new(:name, :eq)
7
+ field.should be_instance_of(described_class)
8
+ field.alias.should == :name
9
+ field.name.should == :name
10
+ field.condition.should == :eq
11
+ end
12
+
13
+ it "should create field with name, condition & options specified" do
14
+ field = described_class.new(:name, :eq, alias: :my_name)
15
+ field.should be_instance_of(described_class)
16
+ field.alias.should == :my_name
17
+ field.name.should == :name
18
+ field.condition.should == :eq
19
+ end
20
+
21
+ it "should raise error if name is not specified" do
22
+ expect{ described_class.new }.to raise_error(ArgumentError)
23
+ end
24
+
25
+ it "should raise error if condition is not specified" do
26
+ expect{ described_class.new(:name) }.to raise_error(ArgumentError)
27
+ end
28
+
29
+ it "should raise error if wrong condition specified" do
30
+ expect{ described_class.new(:name, :my_eq) }.to raise_error(ArgumentError)
31
+ end
32
+ end
33
+
34
+ describe "#==" do
35
+ it "should be equal to obj if it is an instance of Field & it has equal name, condition & alias" do
36
+ f1 = described_class.new(:name, :eq)
37
+ f2 = described_class.new(:name, :eq, alias: :name)
38
+ f1.should == f2
39
+ end
40
+
41
+ it "should not be equal to obj if it is not an instance of Field" do
42
+ f = described_class.new(:name, :eq)
43
+ f.should_not == []
44
+ f.should_not == {}
45
+ f.should_not == 0
46
+ end
47
+
48
+ it "should not be equal to obj if it is an instance of Field & it does not have equal name" do
49
+ described_class.new(:name, :eq).should_not == described_class.new(:my_name, :eq)
50
+ end
51
+
52
+ it "should not be equal to obj if it is an instance of Field & it does not have equal condition" do
53
+ described_class.new(:name, :eq).should_not == described_class.new(:name, :ne)
54
+ end
55
+
56
+ it "should not be equal to obj if it is an instance of Field & it does not have equal alias" do
57
+ described_class.new(:name, :eq).should_not == described_class.new(:name, :eq, alias: :my_name)
58
+ end
59
+ end
60
+ end
@@ -67,6 +67,43 @@ describe FilterFactory::Filter do
67
67
  filter.attributes.should == HashWithIndifferentAccess.new({name: "test name", last_name: nil})
68
68
  end
69
69
 
70
+ it "should raise error if duplicate field definition found" do
71
+ expect do
72
+ described_class.create do
73
+ field :name, :eq
74
+ field :surname, :regex, alias: :last_name
75
+ field :name, :eq, alias: :name
76
+ end
77
+ end.to raise_error(FilterFactory::Filter::DuplicateFieldError)
78
+ end
79
+
80
+ it "should respond to #get_field method" do
81
+ should respond_to(:get_field)
82
+ end
83
+
84
+ it "should return valid field by calling #get_field" do
85
+ filter = described_class.create do
86
+ field :name, :eq
87
+ field :surname, :regex, alias: :last_name
88
+ end
89
+ filter.get_field(:name).should be_instance_of(FilterFactory::Field)
90
+ filter.get_field(:name).condition.should == :eq
91
+ filter.get_field(:surname).should be_instance_of(FilterFactory::Field)
92
+ filter.get_field(:surname).condition.should == :regex
93
+ filter.get_field(:my_name).should be_nil
94
+ end
95
+
96
+ described_class::CONDITIONS.each do |condition|
97
+ it "should respond to #{condition} method" do
98
+ should respond_to(condition)
99
+ end
100
+
101
+ it "should define field with '#{condition}' condition" do
102
+ filter = described_class.create{ public_send(condition, :name) }
103
+ filter.get_field(:name).condition.should == condition
104
+ end
105
+ end
106
+
70
107
  it "should fill filter values from hash" do
71
108
  filter = described_class.create do
72
109
  field :name, :eq
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.2
4
+ version: 0.0.3
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-07 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Gem for easy ActiveRecord/Mongoid models filtering
15
15
  email:
@@ -34,6 +34,7 @@ files:
34
34
  - lib/filter_factory/version.rb
35
35
  - spec/factories/factories.rb
36
36
  - spec/filter_factory/active_record/model_spec.rb
37
+ - spec/filter_factory/field_spec.rb
37
38
  - spec/filter_factory/filter_spec.rb
38
39
  - spec/filter_factory/mongoid/model_spec.rb
39
40
  - spec/models/ar_post.rb
@@ -67,6 +68,7 @@ summary: FilterFactory allows you to easily fetch ActiveRecord/Mongoid models th
67
68
  test_files:
68
69
  - spec/factories/factories.rb
69
70
  - spec/filter_factory/active_record/model_spec.rb
71
+ - spec/filter_factory/field_spec.rb
70
72
  - spec/filter_factory/filter_spec.rb
71
73
  - spec/filter_factory/mongoid/model_spec.rb
72
74
  - spec/models/ar_post.rb