active_hash 0.9.12 → 0.9.13

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 2013-01-22
2
+ - Fix find_by_id and find method returning nil unless .all called in ActiveYaml (mattheworiordan)
3
+
1
4
  2012-07-25
2
5
  - Make find_by_id lookups faster by indexing records by id (desmondmonster)
3
6
 
data/README.md CHANGED
@@ -214,6 +214,35 @@ You can also use standard rails view helpers, like #collection_select:
214
214
 
215
215
  <%= collection_select :person, :country_id, Country.all, :id, :name %>
216
216
 
217
+ ### Using shortcuts
218
+
219
+ Since ActiveHashes usually are static, we can use shortcuts to assign via an easy to remember string instead of an obscure ID number.
220
+
221
+ # app/models/country.rb
222
+ class Country < ActiveHash::Base
223
+ end
224
+
225
+ # app/models/person.rb
226
+ class Person < ActiveRecord::Base
227
+ extend ActiveHash::Associations::ActiveRecordExtensions
228
+ belongs_to_active_hash :country, :shortcuts => [:name]
229
+ end
230
+
231
+ # config/initializers/data.rb
232
+ Country.data = [
233
+ {:id => 1, :name => "US"},
234
+ {:id => 2, :name => "Canada"}
235
+ ]
236
+
237
+ # Using `rails console`
238
+ john = Person.new
239
+ john.country_name = "US"
240
+ # Is the same as doing `john.country = Country.find_by_name("US")`
241
+ john.country_name
242
+ # Will return "US", and is the same as doing `john.country.try(:name)`
243
+
244
+ You can have multiple shortcuts, so settings `:shortcuts => [:name, :friendly_name]` will enable you to use `#country_name=` and `#country_friendly_name=`.
245
+
217
246
  ## Referencing ActiveRecord objects from ActiveHash
218
247
 
219
248
  If you include the ActiveHash::Associations module, you can also create associations from your ActiveHash classes, like so:
data/active_hash.gemspec CHANGED
@@ -26,7 +26,8 @@ Gem::Specification.new do |s|
26
26
  "Vladimir Andrijevik",
27
27
  "Adam Anderson",
28
28
  "Keenan Brock",
29
- "Desmond Bowe"
29
+ "Desmond Bowe",
30
+ "Matthew O'Riordan"
30
31
  ]
31
32
  s.date = %q{2012-01-18}
32
33
  s.email = %q{jeff@zilkey.com}
@@ -54,6 +54,16 @@ module ActiveFile
54
54
  raise "Override Me"
55
55
  end
56
56
 
57
+ def find(*args)
58
+ reload unless data_loaded
59
+ return super
60
+ end
61
+
62
+ def find_by_id(*args)
63
+ reload unless data_loaded
64
+ return super
65
+ end
66
+
57
67
  protected :extension
58
68
 
59
69
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "0.9.12"
3
+ VERSION = "0.9.13"
4
4
  end
5
5
  end
@@ -5,9 +5,11 @@ module ActiveHash
5
5
 
6
6
  def belongs_to_active_hash(association_id, options = {})
7
7
  options = {
8
- :class_name => association_id.to_s.classify,
9
- :foreign_key => association_id.to_s.foreign_key
8
+ :class_name => association_id.to_s.camelize,
9
+ :foreign_key => association_id.to_s.foreign_key,
10
+ :shortcuts => []
10
11
  }.merge(options)
12
+ options[:shortcuts] = [options[:shortcuts]] unless options[:shortcuts].kind_of?(Array)
11
13
 
12
14
  define_method(association_id) do
13
15
  options[:class_name].constantize.find_by_id(send(options[:foreign_key]))
@@ -17,6 +19,16 @@ module ActiveHash
17
19
  send "#{options[:foreign_key]}=", new_value ? new_value.id : nil
18
20
  end
19
21
 
22
+ options[:shortcuts].each do |shortcut|
23
+ define_method("#{association_id}_#{shortcut}") do
24
+ send(association_id).try(shortcut)
25
+ end
26
+
27
+ define_method("#{association_id}_#{shortcut}=") do |new_value|
28
+ send "#{association_id}=", new_value ? options[:class_name].constantize.send("find_by_#{shortcut}", new_value) : nil
29
+ end
30
+ end
31
+
20
32
  create_reflection(
21
33
  :belongs_to,
22
34
  association_id.to_sym,
@@ -82,4 +82,22 @@ describe ActiveYaml::Base do
82
82
 
83
83
  end
84
84
 
85
+ describe 'ID finders without reliance on a call to all, even with fields specified' do
86
+
87
+ before do
88
+ class City < ActiveYaml::Base
89
+ fields :id, :state, :name
90
+ end
91
+ end
92
+
93
+ it 'returns a single city based on #find' do
94
+ City.find(1).name.should == 'Albany'
95
+ end
96
+
97
+ it 'returns a single city based on find_by_id' do
98
+ City.find_by_id(1).name.should == 'Albany'
99
+ end
100
+
101
+ end
102
+
85
103
  end
@@ -26,6 +26,9 @@ describe ActiveHash::Base, "associations" do
26
26
  include ActiveHash::Associations
27
27
  end
28
28
 
29
+ class SchoolStatus < ActiveHash::Base
30
+ end
31
+
29
32
  class Book < ActiveRecord::Base
30
33
  establish_connection :adapter => "sqlite3", :database => ":memory:"
31
34
  connection.create_table(:books, :force => true) do |t|
@@ -120,6 +123,24 @@ describe ActiveHash::Base, "associations" do
120
123
  school.city.should == city
121
124
  end
122
125
 
126
+ it "is assignable by name attribute" do
127
+ School.belongs_to_active_hash :city, :shortcuts => [:name]
128
+ City.data = [ {:id => 1, :name => 'gothan'} ]
129
+ city = City.find_by_name 'gothan'
130
+ school = School.create :city_name => 'gothan'
131
+ school.city.should == city
132
+ school.city_name.should == 'gothan'
133
+ end
134
+
135
+ it "have custom shortcut" do
136
+ School.belongs_to_active_hash :city, :shortcuts => :friendly_name
137
+ City.data = [ {:id => 1, :friendly_name => 'Gothan City'} ]
138
+ city = City.find_by_friendly_name 'Gothan City'
139
+ school = School.create :city_friendly_name => 'Gothan City'
140
+ school.city.should == city
141
+ school.city_friendly_name.should == 'Gothan City'
142
+ end
143
+
123
144
  it "returns nil when the record does not exist" do
124
145
  School.belongs_to_active_hash :city
125
146
  school = School.create! :city => nil
@@ -133,6 +154,13 @@ describe ActiveHash::Base, "associations" do
133
154
  association.should_not be_nil
134
155
  association.klass.name.should == City.name
135
156
  end
157
+
158
+ it "handles classes ending with an 's'" do
159
+ School.belongs_to_active_hash :school_status
160
+ association = School.reflect_on_association(:school_status)
161
+ association.should_not be_nil
162
+ association.klass.name.should == SchoolStatus.name
163
+ end
136
164
  end
137
165
  end
138
166
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.12
4
+ version: 0.9.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -23,14 +23,15 @@ authors:
23
23
  - Adam Anderson
24
24
  - Keenan Brock
25
25
  - Desmond Bowe
26
+ - Matthew O'Riordan
26
27
  autorequire:
27
28
  bindir: bin
28
29
  cert_chain: []
29
- date: 2012-01-18 00:00:00.000000000Z
30
+ date: 2012-01-18 00:00:00.000000000 Z
30
31
  dependencies:
31
32
  - !ruby/object:Gem::Dependency
32
33
  name: activesupport
33
- requirement: &70291036455660 !ruby/object:Gem::Requirement
34
+ requirement: !ruby/object:Gem::Requirement
34
35
  none: false
35
36
  requirements:
36
37
  - - ! '>='
@@ -38,10 +39,15 @@ dependencies:
38
39
  version: 2.2.2
39
40
  type: :runtime
40
41
  prerelease: false
41
- version_requirements: *70291036455660
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.2.2
42
48
  - !ruby/object:Gem::Dependency
43
49
  name: rspec
44
- requirement: &70291036454000 !ruby/object:Gem::Requirement
50
+ requirement: !ruby/object:Gem::Requirement
45
51
  none: false
46
52
  requirements:
47
53
  - - ~>
@@ -49,10 +55,15 @@ dependencies:
49
55
  version: 2.2.0
50
56
  type: :development
51
57
  prerelease: false
52
- version_requirements: *70291036454000
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: 2.2.0
53
64
  - !ruby/object:Gem::Dependency
54
65
  name: sqlite3
55
- requirement: &70291036451580 !ruby/object:Gem::Requirement
66
+ requirement: !ruby/object:Gem::Requirement
56
67
  none: false
57
68
  requirements:
58
69
  - - ! '>='
@@ -60,10 +71,15 @@ dependencies:
60
71
  version: '0'
61
72
  type: :development
62
73
  prerelease: false
63
- version_requirements: *70291036451580
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
64
80
  - !ruby/object:Gem::Dependency
65
81
  name: activerecord
66
- requirement: &70291036450320 !ruby/object:Gem::Requirement
82
+ requirement: !ruby/object:Gem::Requirement
67
83
  none: false
68
84
  requirements:
69
85
  - - ! '>='
@@ -71,10 +87,15 @@ dependencies:
71
87
  version: 2.2.2
72
88
  type: :development
73
89
  prerelease: false
74
- version_requirements: *70291036450320
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: 2.2.2
75
96
  - !ruby/object:Gem::Dependency
76
97
  name: appraisal
77
- requirement: &70291036449280 !ruby/object:Gem::Requirement
98
+ requirement: !ruby/object:Gem::Requirement
78
99
  none: false
79
100
  requirements:
80
101
  - - ! '>='
@@ -82,7 +103,12 @@ dependencies:
82
103
  version: '0'
83
104
  type: :development
84
105
  prerelease: false
85
- version_requirements: *70291036449280
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
86
112
  description:
87
113
  email: jeff@zilkey.com
88
114
  executables: []
@@ -130,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
156
  version: '0'
131
157
  requirements: []
132
158
  rubyforge_project:
133
- rubygems_version: 1.8.10
159
+ rubygems_version: 1.8.24
134
160
  signing_key:
135
161
  specification_version: 3
136
162
  summary: An ActiveRecord-like model that uses a hash or file as a datasource