active_hash 0.9.5 → 0.9.6

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/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 2011-08-31
2
+ - added a module which adds a .belongs_to_active_hash method to ActiveRecord, since it was broken for Rails 3.1 (thanks to felixclack for pointing this issue out)
3
+
1
4
  2011-06-07
2
5
  - fixed bug where .find would not work if you defined your ids as strings
3
6
 
data/README.md CHANGED
@@ -167,9 +167,11 @@ To clear all records from the in-memory array, call delete_all:
167
167
 
168
168
  Country.delete_all # => does not affect the yaml files in any way - just clears the in-memory array which can be useful for testing
169
169
 
170
- ## Associations
170
+ ## Referencing ActiveHash objects from ActiveRecord Associations
171
171
 
172
- You can create has_many and belongs_to associations to and from ActiveRecord. Out of the box, you can create .belongs_to associations from rails objects, like so:
172
+ One common use case for ActiveHash is to have top-level objects in memory that ActiveRecord objects belong to.
173
+
174
+ In versions of ActiveRecord previous to 3.1, you should be able to do the following:
173
175
 
174
176
  class Country < ActiveHash::Base
175
177
  end
@@ -178,7 +180,17 @@ You can create has_many and belongs_to associations to and from ActiveRecord. O
178
180
  belongs_to :country
179
181
  end
180
182
 
181
- ActiveHash will also work as a polymorphic parent:
183
+ However, as of ActiveRecord 3.1 support for ActiveRecord's `belong_to` is broken. Instead, you must use the `belongs_to_active_hash` method:
184
+
185
+ class Country < ActiveHash::Base
186
+ end
187
+
188
+ class Person < ActiveRecord::Base
189
+ extend ActiveHash::Associations::ActiveRecordExtensions
190
+ belongs_to_active_hash :country
191
+ end
192
+
193
+ With ActiveRecord versions < 3.1, ActiveHash will also work as a polymorphic parent:
182
194
 
183
195
  class Country < ActiveHash::Base
184
196
  end
@@ -192,10 +204,14 @@ ActiveHash will also work as a polymorphic parent:
192
204
  person.save
193
205
  person.location # => Country.first
194
206
 
207
+ However, as of ActiveRecord 3.1 this will not work. If you need support for that, please open an issue.
208
+
195
209
  You can also use standard rails view helpers, like #collection_select:
196
210
 
197
211
  <%= collection_select :person, :country_id, Country.all, :id, :name %>
198
212
 
213
+ ## Referencing ActiveRecord objects from ActiveHash
214
+
199
215
  If you include the ActiveHash::Associations module, you can also create associations from your ActiveHash classes, like so:
200
216
 
201
217
  class Country < ActiveHash::Base
@@ -225,8 +241,6 @@ Once you define a belongs to, you also get the setter method:
225
241
 
226
242
  NOTE: You cannot use ActiveHash objects as children of ActiveRecord and I don't plan on adding support for that. It doesn't really make any sense, since you'd have to hard-code your database ids in your class or yaml files, which is a dependency inversion.
227
243
 
228
- Also, the implementation of has_many and belongs_to is very simple - I hope to add better support for it later - it will only work in the trivial cases for now.
229
-
230
244
  Thanks to baldwindavid for the ideas and code on that one.
231
245
 
232
246
  ## ActiveYaml
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
  "Vladimir Andrijevik",
27
27
  "Adam Anderson"
28
28
  ]
29
- s.date = %q{2011-01-22}
29
+ s.date = %q{2011-08-31}
30
30
  s.email = %q{jeff@zilkey.com}
31
31
  s.extra_rdoc_files = [
32
32
  "LICENSE",
@@ -24,6 +24,11 @@ module ActiveHash
24
24
  end
25
25
 
26
26
  class << self
27
+
28
+ def primary_key
29
+ "id"
30
+ end
31
+
27
32
  def field_names
28
33
  @field_names ||= []
29
34
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "0.9.5"
3
+ VERSION = "0.9.6"
4
4
  end
5
5
  end
@@ -1,6 +1,25 @@
1
1
  module ActiveHash
2
2
  module Associations
3
3
 
4
+ module ActiveRecordExtensions
5
+
6
+ def belongs_to_active_hash(association_id, options = {})
7
+ options = {
8
+ :class_name => association_id.to_s.classify,
9
+ :foreign_key => association_id.to_s.foreign_key
10
+ }.merge(options)
11
+
12
+ define_method(association_id) do
13
+ options[:class_name].constantize.find_by_id(send(options[:foreign_key]))
14
+ end
15
+
16
+ define_method("#{association_id}=") do |new_value|
17
+ attributes[options[:foreign_key].to_sym] = new_value ? new_value.id : nil
18
+ end
19
+ end
20
+
21
+ end
22
+
4
23
  def self.included(base)
5
24
  base.extend Methods
6
25
  end
@@ -17,7 +36,7 @@ module ActiveHash
17
36
  klass = options[:class_name].constantize
18
37
 
19
38
  if klass.respond_to?(:scoped)
20
- klass.scoped(:conditions => { options[:foreign_key] => id })
39
+ klass.scoped(:conditions => {options[:foreign_key] => id})
21
40
  else
22
41
  klass.send("find_all_by_#{options[:foreign_key]}", id)
23
42
  end
@@ -38,7 +57,7 @@ module ActiveHash
38
57
  end
39
58
 
40
59
  define_method("#{association_id}=") do |new_value|
41
- attributes[ options[:foreign_key].to_sym ] = new_value ? new_value.id : nil
60
+ attributes[options[:foreign_key].to_sym] = new_value ? new_value.id : nil
42
61
  end
43
62
 
44
63
  end
@@ -1,14 +1,17 @@
1
1
  require 'spec_helper'
2
+ require 'active_record'
2
3
 
3
4
  describe ActiveHash::Base, "associations" do
4
5
 
5
6
  before do
6
7
  class Country < ActiveRecord::Base
8
+ extend ActiveHash::Associations::ActiveRecordExtensions
7
9
  establish_connection :adapter => "sqlite3", :database => ":memory:"
8
10
  connection.create_table(:countries, :force => true) {}
9
11
  end
10
12
 
11
13
  class School < ActiveRecord::Base
14
+ extend ActiveHash::Associations::ActiveRecordExtensions
12
15
  establish_connection :adapter => "sqlite3", :database => ":memory:"
13
16
  connection.create_table(:schools, :force => true) do |t|
14
17
  t.integer :city_id
@@ -91,23 +94,27 @@ describe ActiveHash::Base, "associations" do
91
94
 
92
95
  end
93
96
 
94
- describe "#belongs_to" do
97
+ describe ActiveHash::Associations::ActiveRecordExtensions do
95
98
 
96
- context "with an ActiveRecord child" do
99
+ describe "#belongs_to_active_hash" do
97
100
  it "finds the correct records" do
98
- School.belongs_to :city
101
+ School.belongs_to_active_hash :city
99
102
  city = City.create
100
103
  school = School.create :city_id => city.id
101
104
  school.city.should == city
102
105
  end
103
106
 
104
107
  it "returns nil when the record does not exist" do
105
- School.belongs_to :city
106
- school = School.create :city_id => nil
108
+ School.belongs_to_active_hash :city
109
+ school = School.create! :city_id => nil
107
110
  school.city.should be_nil
108
111
  end
109
112
  end
110
113
 
114
+ end
115
+
116
+ describe "#belongs_to" do
117
+
111
118
  context "with an ActiveRecord parent" do
112
119
  it "find the correct records" do
113
120
  City.belongs_to :country
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 5
10
- version: 0.9.5
9
+ - 6
10
+ version: 0.9.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeff Dean
@@ -30,11 +30,13 @@ autorequire:
30
30
  bindir: bin
31
31
  cert_chain: []
32
32
 
33
- date: 2011-01-22 00:00:00 -07:00
33
+ date: 2011-08-31 00:00:00 -06:00
34
34
  default_executable:
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
- version_requirements: &id001 !ruby/object:Gem::Requirement
37
+ name: activesupport
38
+ prerelease: false
39
+ requirement: &id001 !ruby/object:Gem::Requirement
38
40
  none: false
39
41
  requirements:
40
42
  - - ">="
@@ -45,10 +47,8 @@ dependencies:
45
47
  - 2
46
48
  - 2
47
49
  version: 2.2.2
48
- requirement: *id001
49
- prerelease: false
50
50
  type: :runtime
51
- name: activesupport
51
+ version_requirements: *id001
52
52
  description:
53
53
  email: jeff@zilkey.com
54
54
  executables: []