rails_lookup 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/Rakefile CHANGED
@@ -2,13 +2,13 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('rails_lookup', '0.0.2') do |s|
5
+ Echoe.new('rails_lookup', '0.0.3') do |s|
6
6
  s.description = File.read(File.join(File.dirname(__FILE__), 'README'))
7
7
  s.summary = "Lookup table macro for ActiveRecords"
8
8
  s.url = "http://github.com/Nimster/RailsLookup/"
9
9
  s.author = "Nimrod Priell"
10
10
  s.email = "@nimrodpriell" #Twitter
11
- s.ignore_pattern = ["tmp/*", "script/*"]
11
+ s.ignore_pattern = ["tmp/*", "script/*", "Manifest"]
12
12
  s.development_dependencies = []
13
13
  end
14
14
 
@@ -105,27 +105,27 @@ module ActiveRecord
105
105
  #Rails ActiveRecord support:
106
106
  @cls_for_name = {} if @cls_for_name.nil?
107
107
  @cls_for_name[as_name.to_s.to_sym] = cls
108
- #We need to override this for the dynamic finders.
109
- def method_missing(method_id, *arguments, &block)
110
- if match = ActiveRecord::DynamicFinderMatch.match(method_id)
111
- #reroute kind to _kind_id
112
- idxs = match.attribute_names.collect.with_index { |n,i| [i,n.to_sym] if @cls_for_name.has_key? n.to_sym }.compact
113
- idxs.each do |elm|
114
- if method_id =~ /^find_by_/
115
- arguments[elm[0]] = @cls_for_name[elm[1]].gen_id_for arguments[elm[0]] #Change the argument
116
- end
117
- end
118
- end
119
- super
120
- end
121
108
 
122
- def where(opts, *rest)
123
- #Change the values of the keys in the lookup to their ids
124
- mapped = opts.map { |k,v| @cls_for_name.has_key?(k) ? [k, @cls_for_name[k].id_for(v)] : [k, v] }
125
- opts = Hash[mapped]
109
+ #This makes find_and_create_by, find_all and where methods all work as
110
+ #they should. This will not work if you use reset_column_information,
111
+ #like if you use find_by_session_id in SessionStore. You must redefine
112
+ #it so it re-sets rel to this singleton object
113
+ rel = ActiveRecord::Relation.new(self, arel_table)
114
+ def rel.where(opts, *rest)
115
+ if opts.is_a? Hash
116
+ mapped = opts.map { |k,v| @cls_for_name.has_key?(k.to_sym) ? [k, @cls_for_name[k.to_sym].id_for(v)] : [k, v] }
117
+ opts = Hash[mapped]
118
+ end
126
119
  super(opts, *rest)
127
120
  end
128
121
 
122
+ def rel.cls_for_name=(cls_for_name)
123
+ @cls_for_name = cls_for_name
124
+ end
125
+
126
+ rel.cls_for_name = @cls_for_name
127
+ instance_variable_set(:@relation, rel)
128
+
129
129
  #Might need to be in class_eval
130
130
  belongs_to lookup_name.to_s.to_sym, :foreign_key => "#{as_name}_id"
131
131
  validates as_name.to_s.to_sym, :presence => true
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rails_lookup}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [%q{Nimrod Priell}]
9
- s.date = %q{2011-08-08}
9
+ s.date = %q{2011-08-09}
10
10
  s.description = %q{Lookup tables with ruby-on-rails
11
11
  --------------------------------
12
12
 
@@ -152,7 +152,7 @@ I hope this helped you and saved a lot of time and frustration. Follow me on twi
152
152
  }
153
153
  s.email = %q{@nimrodpriell}
154
154
  s.extra_rdoc_files = [%q{README}, %q{lib/active_record/lookup.rb}]
155
- s.files = [%q{README}, %q{Rakefile}, %q{lib/active_record/lookup.rb}, %q{rails_lookup.gemspec}, %q{test/20110808002412_create_test_lookup_db.rb}, %q{test/test_lookup.rb} ]
155
+ s.files = [%q{README}, %q{Rakefile}, %q{lib/active_record/lookup.rb}, %q{rails_lookup.gemspec}, %q{test/20110808002412_create_test_lookup_db.rb}, %q{test/test_lookup.rb}]
156
156
  s.homepage = %q{http://github.com/Nimster/RailsLookup/}
157
157
  s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{Rails_lookup}, %q{--main}, %q{README}]
158
158
  s.require_paths = [%q{lib}]
@@ -120,29 +120,53 @@ class TestLookup < MiniTest::Unit::TestCase
120
120
  bimba2 = Car.find_by_kind "Compact"
121
121
  assert_equal bimba, bimba2
122
122
  assert_equal "Yellow", bimba2.color
123
+ cars = Car.find_all_by_color "Yellow"
124
+ assert_equal 2, cars.size
125
+ cars = Car.find_all_by_kind "Compact"
126
+ assert_equal 1, cars.size
123
127
  bimba2 = Car.find_by_kind_and_color_and_name "Compact", "Yellow", "Bimba"
124
128
  assert_equal bimba, bimba2
125
129
  assert_equal "Yellow", bimba2.color
126
130
  assert_equal "Compact", bimba2.kind
131
+ cars_before_creation = Car.count
127
132
  susita = Car.find_or_create_by_name_and_kind_and_color "Susita", "Compact", "Gray"
128
133
  assert_equal "Gray", susita.color
129
134
  assert_equal "Compact", susita.kind
135
+ assert_equal cars_before_creation + 1, Car.count
130
136
  bimba2 = Car.find_or_create_by_name_and_kind_and_color "Bimba", "Compact", "Yellow"
131
137
  assert_equal "Yellow", bimba2.color
132
138
  assert_equal "Compact", bimba2.kind
133
139
  assert_equal "Bimba", bimba2.name
140
+ assert_equal cars_before_creation + 1, Car.count
141
+ p Car.all
134
142
  f16 = Plane.find_or_create_by_kind_and_name "Fighter Jet", "F-16"
135
143
  assert_equal "Fighter Jet", f16.kind
136
144
  assert_equal "F-16", f16.name
137
145
  ferrari = Car.find_or_create_by_kind_and_color "Sports", "Yellow"
138
146
  assert_equal "Yellow", ferrari.color
139
147
  assert_equal "Sports", ferrari.kind
148
+ assert_equal cars_before_creation + 1, Car.count
140
149
  batmobile = Car.find_or_create_by_kind_and_color "Fantasy", "Black"
141
150
  assert_equal "Black", batmobile.color
142
151
  assert_equal "Fantasy", batmobile.kind
152
+ assert_equal cars_before_creation + 2, Car.count
143
153
  assert_equal 3, CarKind.count
144
154
  assert_equal 3, CarColor.count
145
155
  assert_equal 1, PlaneKind.count
156
+ batmobile2 = Car.find_or_initialize_by_color "Black"
157
+ refute batmobile.new_record?
158
+ assert_equal batmobile, batmobile2
159
+ batmobile2 = Car.find_or_initialize_by_color_and_kind "Black", "Fantasy"
160
+ refute batmobile.new_record?
161
+ assert_equal batmobile, batmobile2
162
+ bimba2 = Car.find_or_initialize_by_name "Bimba"
163
+ refute bimba2.new_record?
164
+ assert_equal bimba, bimba2
165
+ delorhean = Car.find_or_initialize_by_color_and_kind "Grey", "Fantasy"
166
+ assert delorhean.new_record?
167
+ assert_equal "Grey", delorhean.color
168
+ assert_equal "Fantasy", delorhean.kind
169
+ delorhean.save!
146
170
  end
147
171
 
148
172
  #This test will issue warnings as we re-set existing constants
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rails_lookup
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nimrod Priell
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-08 00:00:00 Z
13
+ date: 2011-08-09 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: "Lookup tables with ruby-on-rails\n\