searchgasm 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1 +1,3 @@
1
+ v0.9.1 Added aliases for datetime, date, time, and timestamp attrs. You could call created_at_after, mow you can also call created_after.
2
+
1
3
  v0.9.0. First release
data/README.mdown CHANGED
@@ -34,17 +34,16 @@ Now go into your console and try out any of these example with your own models.
34
34
 
35
35
  User.all(
36
36
  :conditions => {
37
- :first_name_contains => "Ben",
38
- :email_ends_with => "binarylogic.com"
37
+ :first_name_contains => "Ben", # first_name like '%Ben%'
38
+ :email_ends_with => "binarylogic.com" # email like '%binarylogic.com'
39
39
  },
40
- :page => 3,
41
- :per_page => 20
40
+ :per_page => 20 # limit 20
41
+ :page => 3 # offset 60
42
42
  )
43
43
 
44
44
  ## Detailed Example w/ object based searching
45
45
 
46
- # new_search returns an object, you can call "find", "all", "first" also, see "different ways to search" below
47
-
46
+ # Instantiate
48
47
  search = User.new_search(
49
48
  :conditions => {
50
49
  :first_name_contains => "Ben",
@@ -56,11 +55,15 @@ Now go into your console and try out any of these example with your own models.
56
55
  :order_by => {:orders => :total},
57
56
  :order_as => "DESC"
58
57
  )
58
+
59
+ # Set conditions on relationships
59
60
  search.conditions.email_ends_with = "binarylogic.com"
60
- search.conditions.oders.line_items.created_at_after = Time.now
61
+ search.conditions.oders.line_items.created_after = Time.now
62
+
63
+ # Set options
61
64
  search.per_page = 50 # overrides the 20 set above
62
65
 
63
- # Call ANY of the ActiveRecord options
66
+ # Set ANY of the ActiveRecord options
64
67
  search.group = "last_name"
65
68
  search.readonly = true
66
69
  # ... see ActiveRecord documentation
@@ -98,7 +101,7 @@ Any of the options used in the above example can be used in these, but for the s
98
101
 
99
102
  User.find(:first, :conditions => {::age_gt => 18}, :per_page => 20)
100
103
 
101
- search = User.new_search(:conditions => {:age_gt => 18})
104
+ search = User.new_search(:conditions => {:age_gt => 18}) # build_search is an alias
102
105
  search.conditions.first_name_contains = "Ben"
103
106
  search.per_page = 20
104
107
  search.all
@@ -226,7 +229,7 @@ After all of that, here's why I love this plugin:
226
229
 
227
230
  <% form_for :search, User.new_conditions, :url => users_path do |f| %>
228
231
  <%= f.text_field :first_name_contains %>
229
- <%= f.calendar_date_select :created_at_after %>
232
+ <%= f.calendar_date_select :created_after %>
230
233
  <%= f.select :age_gt, (1..100) %>
231
234
  <% end %>
232
235
 
@@ -7,19 +7,19 @@ module BinaryLogic
7
7
  attr_accessor :klass, :relationship_name, :scope
8
8
 
9
9
  class << self
10
- def conditions_for_column_type(type)
11
- condition_names = [:equals, :does_not_equal]
10
+ def condition_types_for_column_type(type)
11
+ condition_types = [:equals, :does_not_equal]
12
12
  case type
13
13
  when :string, :text
14
- condition_names += [:begins_with, :contains, :keywords, :ends_with]
14
+ condition_types += [:begins_with, :contains, :keywords, :ends_with]
15
15
  when :integer, :float, :decimal, :datetime, :timestamp, :time, :date
16
- condition_names += [:greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to]
16
+ condition_types += [:greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to]
17
17
  end
18
- condition_names
18
+ condition_types
19
19
  end
20
-
21
- def alias_conditions(condition)
22
- case condition
20
+
21
+ def aliases_for_condition_type(condition_type)
22
+ case condition_type
23
23
  when :equals then ["", :is]
24
24
  when :does_not_equal then [:is_not, :not]
25
25
  when :begins_with then [:starts_with]
@@ -31,6 +31,28 @@ module BinaryLogic
31
31
  else []
32
32
  end
33
33
  end
34
+
35
+ def aliases_for_condition(*args)
36
+ column, condition_type = nil, nil
37
+
38
+ # Allow a condition object or the column and condition type to be passed
39
+ if args.size == 1
40
+ column, condition_type = condition.column, condition.condition
41
+ else
42
+ column, condition_type = args.first, args[1]
43
+ end
44
+
45
+ name = Condition.generate_name(column, condition_type)
46
+ alias_condition_types = aliases_for_condition_type(condition_type)
47
+ column_names = [column.name]
48
+ column_names << column.name.gsub(/_at$/, "") if [:datetime, :timestamp, :time, :date].include?(column.type) && column.name =~ /_at$/
49
+
50
+ aliases = []
51
+ column_names.each do |column_name|
52
+ alias_condition_types.each { |alias_condition_type| aliases << Condition.generate_name(column_name, alias_condition_type) }
53
+ end
54
+ aliases
55
+ end
34
56
  end
35
57
 
36
58
  def initialize(klass, values = {})
@@ -116,17 +138,17 @@ module BinaryLogic
116
138
  end
117
139
 
118
140
  def add_conditions_for_column!(column)
119
- self.class.conditions_for_column_type(column.type).collect { |condition_name| add_condition!(condition_name, column) }
141
+ self.class.condition_types_for_column_type(column.type).collect { |condition_type| add_condition!(condition_type, column) }
120
142
  end
121
143
 
122
- def add_condition!(condition_name, column)
123
- name = Condition.generate_name(column, condition_name)
144
+ def add_condition!(condition_type, column)
145
+ name = Condition.generate_name(column, condition_type)
124
146
 
125
147
  # Define accessor methods
126
148
  self.class.class_eval <<-SRC
127
149
  def #{name}_object
128
150
  if @#{name}.nil?
129
- @#{name} = Condition.new(:#{condition_name}, klass, "#{column.name}")
151
+ @#{name} = Condition.new(:#{condition_type}, klass, "#{column.name}")
130
152
  self.objects << @#{name}
131
153
  end
132
154
  @#{name}
@@ -138,8 +160,7 @@ module BinaryLogic
138
160
  SRC
139
161
 
140
162
  # Define aliases
141
- self.class.alias_conditions(condition_name).each do |alias_condition_name|
142
- alias_name = Condition.generate_name(column, alias_condition_name)
163
+ self.class.aliases_for_condition(column, condition_type).each do |alias_name|
143
164
  self.class.class_eval do
144
165
  alias_method alias_name, name
145
166
  alias_method "#{alias_name}=", "#{name}="
@@ -68,7 +68,7 @@ module BinaryLogic
68
68
 
69
69
  MAJOR = 0
70
70
  MINOR = 9
71
- TINY = 0
71
+ TINY = 1
72
72
 
73
73
  # The current version as a Version instance
74
74
  CURRENT = new(MAJOR, MINOR, TINY)
data/searchgasm.gemspec CHANGED
@@ -1,11 +1,11 @@
1
1
 
2
- # Gem::Specification for Searchgasm-0.9.0
2
+ # Gem::Specification for Searchgasm-0.9.1
3
3
  # Originally generated by Echoe
4
4
 
5
5
  --- !ruby/object:Gem::Specification
6
6
  name: searchgasm
7
7
  version: !ruby/object:Gem::Version
8
- version: 0.9.0
8
+ version: 0.9.1
9
9
  platform: ruby
10
10
  authors:
11
11
  - Ben Johnson of Binary Logic
@@ -24,14 +24,13 @@ class TestSearchgasmConditions < Test::Unit::TestCase
24
24
 
25
25
  klass.columns.each do |column|
26
26
  value = column_value(column)
27
- BinaryLogic::Searchgasm::Search::Conditions.conditions_for_column_type(column.type).each do |condition|
28
- name = BinaryLogic::Searchgasm::Search::Condition.generate_name(column, condition)
27
+ BinaryLogic::Searchgasm::Search::Conditions.condition_types_for_column_type(column.type).each do |condition_type|
28
+ name = BinaryLogic::Searchgasm::Search::Condition.generate_name(column, condition_type)
29
29
  conditions.send("#{name}=", value)
30
30
  assert_equal conditions.send(name), value
31
- BinaryLogic::Searchgasm::Search::Conditions.alias_conditions(condition).each do |alias_condition|
32
- alias_name = BinaryLogic::Searchgasm::Search::Condition.generate_name(column, alias_condition)
33
- conditions.send("#{alias_name}=", value)
34
- assert_equal conditions.send(alias_name), value
31
+ BinaryLogic::Searchgasm::Search::Conditions.aliases_for_condition(column, condition_type).each do |alias_condition|
32
+ conditions.send("#{alias_condition}=", value)
33
+ assert_equal conditions.send(alias_condition), value
35
34
  end
36
35
  end
37
36
  end
@@ -98,7 +97,7 @@ class TestSearchgasmConditions < Test::Unit::TestCase
98
97
  conditions.name_contains = "Binary"
99
98
  conditions.id_gt = 5
100
99
  now = Time.now
101
- conditions.created_at_after = now
100
+ conditions.created_after = now
102
101
  assert_equal conditions.sanitize, ["(\"accounts\".\"name\" LIKE ?) AND (\"accounts\".\"id\" > ?) AND (\"accounts\".\"created_at\" > ?)", "%Binary%", 5, now]
103
102
 
104
103
  # test out associations
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchgasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Johnson of Binary Logic