active_hash 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,20 +3,17 @@ module ActiveHash
3
3
 
4
4
  module ActiveRecordExtensions
5
5
 
6
- def belongs_to(*args)
7
- our_args = args.dup
8
- options = our_args.extract_options!
9
- name = our_args.shift
10
- options = {:class_name => name.to_s.camelize }.merge(options)
6
+ def belongs_to(name, scope = nil, **options)
7
+ klass_name = options.key?(:class_name) ? options[:class_name] : name.to_s.camelize
11
8
  klass =
12
9
  begin
13
- options[:class_name].constantize
14
- rescue
15
- nil
16
- rescue LoadError
10
+ klass_name.constantize
11
+ rescue StandardError, LoadError
17
12
  nil
18
13
  end
14
+
19
15
  if klass && klass < ActiveHash::Base
16
+ options = { class_name: klass_name }.merge(options)
20
17
  belongs_to_active_hash(name, options)
21
18
  else
22
19
  super
@@ -52,13 +49,20 @@ module ActiveHash
52
49
  end
53
50
 
54
51
  if ActiveRecord::Reflection.respond_to?(:create)
55
- reflection = ActiveRecord::Reflection.create(
56
- :belongs_to,
57
- association_id.to_sym,
58
- nil,
59
- options,
60
- self
61
- )
52
+ if defined?(ActiveHash::Reflection::BelongsToReflection)
53
+ reflection = ActiveHash::Reflection::BelongsToReflection.new(association_id.to_sym, nil, options, self)
54
+ if options[:through]
55
+ reflection = ActiveRecord::ThroughReflection.new(reflection)
56
+ end
57
+ else
58
+ reflection = ActiveRecord::Reflection.create(
59
+ :belongs_to,
60
+ association_id.to_sym,
61
+ nil,
62
+ options,
63
+ self
64
+ )
65
+ end
62
66
 
63
67
  ActiveRecord::Reflection.add_reflection(
64
68
  self,
@@ -88,12 +92,12 @@ module ActiveHash
88
92
  end
89
93
 
90
94
  def self.included(base)
95
+ require_relative "reflection_extensions"
91
96
  base.extend Methods
92
97
  end
93
98
 
94
99
  module Methods
95
100
  def has_many(association_id, options = {})
96
-
97
101
  define_method(association_id) do
98
102
  options = {
99
103
  :class_name => association_id.to_s.classify,
@@ -113,13 +117,17 @@ module ActiveHash
113
117
  klass.where(foreign_key => primary_key_value)
114
118
  end
115
119
  end
120
+ define_method("#{association_id.to_s.underscore.singularize}_ids") do
121
+ public_send(association_id).map(&:id)
122
+ end
116
123
  end
117
124
 
118
125
  def has_one(association_id, options = {})
119
126
  define_method(association_id) do
120
127
  options = {
121
128
  :class_name => association_id.to_s.classify,
122
- :foreign_key => self.class.to_s.foreign_key
129
+ :foreign_key => self.class.to_s.foreign_key,
130
+ :primary_key => self.class.primary_key
123
131
  }.merge(options)
124
132
 
125
133
  scope = options[:class_name].constantize
@@ -127,7 +135,7 @@ module ActiveHash
127
135
  if scope.respond_to?(:scoped) && options[:conditions]
128
136
  scope = scope.scoped(:conditions => options[:conditions])
129
137
  end
130
- scope.send("find_by_#{options[:foreign_key]}", id)
138
+ scope.send("find_by_#{options[:foreign_key]}", send(options[:primary_key]))
131
139
  end
132
140
  end
133
141
 
@@ -0,0 +1,25 @@
1
+ module ActiveHash
2
+ module Reflection
3
+ class BelongsToReflection < ActiveRecord::Reflection::BelongsToReflection
4
+ def compute_class(name)
5
+ if polymorphic?
6
+ raise ArgumentError, "Polymorphic associations do not support computing the class."
7
+ end
8
+
9
+ begin
10
+ klass = active_record.send(:compute_type, name)
11
+ rescue NameError => error
12
+ if error.name.match?(/(?:\A|::)#{name}\z/)
13
+ message = "Missing model class #{name} for the #{active_record}##{self.name} association."
14
+ message += " You can specify a different model class with the :class_name option." unless options[:class_name]
15
+ raise NameError.new(message, name)
16
+ else
17
+ raise
18
+ end
19
+ end
20
+
21
+ klass
22
+ end
23
+ end
24
+ end
25
+ end
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: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -26,10 +26,10 @@ authors:
26
26
  - Brett Richardson
27
27
  - Rachel Heaton
28
28
  - Keisuke Izumiya
29
- autorequire:
29
+ autorequire:
30
30
  bindir: bin
31
31
  cert_chain: []
32
- date: 2020-01-15 00:00:00.000000000 Z
32
+ date: 2023-05-06 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: activesupport
@@ -66,7 +66,7 @@ executables: []
66
66
  extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
- - CHANGELOG
69
+ - CHANGELOG.md
70
70
  - LICENSE
71
71
  - README.md
72
72
  - active_hash.gemspec
@@ -75,18 +75,25 @@ files:
75
75
  - lib/active_file/multiple_files.rb
76
76
  - lib/active_hash.rb
77
77
  - lib/active_hash/base.rb
78
+ - lib/active_hash/condition.rb
79
+ - lib/active_hash/conditions.rb
78
80
  - lib/active_hash/relation.rb
79
81
  - lib/active_hash/version.rb
80
82
  - lib/active_json/base.rb
81
83
  - lib/active_yaml/aliases.rb
82
84
  - lib/active_yaml/base.rb
83
85
  - lib/associations/associations.rb
86
+ - lib/associations/reflection_extensions.rb
84
87
  - lib/enum/enum.rb
85
- homepage: http://github.com/zilkey/active_hash
88
+ homepage: http://github.com/active-hash/active_hash
86
89
  licenses:
87
90
  - MIT
88
- metadata: {}
89
- post_install_message:
91
+ metadata:
92
+ homepage_uri: http://github.com/active-hash/active_hash
93
+ changelog_uri: https://github.com/active-hash/active_hash/blob/master/CHANGELOG.md
94
+ source_code_uri: http://github.com/active-hash/active_hash
95
+ bug_tracker_uri: https://github.com/active-hash/active_hash/issues
96
+ post_install_message:
90
97
  rdoc_options: []
91
98
  require_paths:
92
99
  - lib
@@ -101,9 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
108
  - !ruby/object:Gem::Version
102
109
  version: '0'
103
110
  requirements: []
104
- rubyforge_project:
105
- rubygems_version: 2.7.6
106
- signing_key:
111
+ rubygems_version: 3.2.33
112
+ signing_key:
107
113
  specification_version: 4
108
114
  summary: An ActiveRecord-like model that uses a hash or file as a datasource
109
115
  test_files: []
data/CHANGELOG DELETED
@@ -1,225 +0,0 @@
1
- 2020-01-15 (v3.1.0)
2
- - Add ActiveHash::Base.order method inspired by ActiveRecord [#177](https://github.com/zilkey/active_hash/pull/177)
3
- - Add #to_ary to ActiveHash::Relation [#182](https://github.com/zilkey/active_hash/pull/182)
4
- - Allow #find to behave like Enumerable#find if id is nil and a block is given [#183](https://github.com/zilkey/active_hash/pull/183)
5
- - Delegate :sample to `records` [#189](https://github.com/zilkey/active_hash/pull/189)
6
-
7
- 2019-09-28 (v3.0.0)
8
- - Make #where chainable [#178](https://github.com/zilkey/active_hash/pull/178)
9
-
10
- 2019-09-28 (v2.3.0)
11
- - Add ::scope method (inspired by ActiveRecord) [#173](https://github.com/zilkey/active_hash/pull/173)
12
- - Let `.find(nil)` raise ActiveHash::RecordNotFound (inspired by ActiveRecord) [#174](https://github.com/zilkey/active_hash/pull/174)
13
- - `where` clause now works with range argument [#175](https://github.com/zilkey/active_hash/pull/175)
14
-
15
- 2019-03-06 (v2.2.1)
16
- - Allow empty YAML [#171](https://github.com/zilkey/active_hash/pull/171) Thanks, @ppworks
17
-
18
- 2018-11-22 (v2.2.0)
19
- - Support pluck method [#164](https://github.com/zilkey/active_hash/pull/164) Thanks, @ihatov08
20
- - Support where.not method [#167](https://github.com/zilkey/active_hash/pull/167) Thanks, @DialBird
21
-
22
- 2018-04-05 (v2.1.0)
23
- - Allow to use ERB (embedded ruby) in yml files [#160](https://github.com/zilkey/active_hash/pull/160) Thanks, @UgoMare
24
- - Add `ActiveHash::Base.polymorphic_name` [#162](https://github.com/zilkey/active_hash/pull/162)
25
- - Fix to be able to use enum accessor constant with same name as top-level constant[#161](https://github.com/zilkey/active_hash/pull/161) Thanks, @yujideveloper
26
-
27
- 2018-02-27 (v2.0.0)
28
- - Drop old Ruby and Rails support [#157](https://github.com/zilkey/active_hash/pull/157)
29
- - Don't generate instance accessors for class attributes [#136](https://github.com/zilkey/active_hash/pull/136) Thanks, @rainhead
30
-
31
- 2017-06-14 (v1.5.3)
32
- - Support symbol values in where and find_by [#156](https://github.com/zilkey/active_hash/pull/156) Thanks, @south37
33
-
34
- 2017-06-14 (v1.5.2)
35
- - Fix find_by when passed an invalid id [#152](https://github.com/zilkey/active_hash/pull/152) Thanks, @davidstosik
36
-
37
- 2017-04-20 (v1.5.1)
38
- - Fix a bug on `.where` [#147](https://github.com/zilkey/active_hash/pull/147)
39
-
40
- 2017-03-24 (v1.5.0)
41
- - add support for `.find_by!`(@syguer)
42
-
43
- 2015-09-13 (v1.4.1)
44
- - fix bug where `#attributes` didn't contain default values #107
45
- - add support for `.find_by` and `#_read_attribute`. Thanks, @andrewfader!
46
-
47
- 2014-09-03 (v1.4.0)
48
- - support Rails 4.2 (agraves, al2o3cr)
49
-
50
- 2014-02-18 (v1.3.0)
51
- - fix bug where including ActiveHash associations would make `belongs_to :imageable, polymorphic: true` blow up
52
- - fixed several bugs that prevented active hash from being used without active record / active model
53
- - add support for splitting up data sources into multiple files (rheaton)
54
- - add support for storing data in json files (rheaton)
55
-
56
- 2013-11-29 (v1.2.3)
57
- - fix bug where active hash would call `.all` on models when setting has_many (grosser)
58
-
59
- 2013-11-05 (v1.2.2)
60
- - fix bug in gemspec that made it impossible to use w/ Rails 4
61
-
62
- 2013-10-24 (v1.2.1)
63
- - fixed nasty bug in belongs_to that would prevent users from passing procs (thanks for the issue freebird0221)
64
- - fixed bug where passing in a separate class name to belongs_to_active_hash would raise an exception (mauriciopasquier)
65
-
66
- 2013-10-01 (v1.2.0)
67
- - belongs_to is back!
68
- - added support for primary key options for belongs_to (tomtaylor)
69
-
70
- 2013-09-09 (v1.0.2)
71
- - `where(nil)` returns all results, like ActiveRecord (kugaevsky)
72
-
73
- 2013-07-15 (v1.0.1)
74
- - Travis CI for ActiveHash + Ruby 2, 1.8.7, Rubinius and JRuby support (mattheworiordan)
75
- - no longer need to call .all before executing `find_by_*` or `where` methods (mattheworiordan)
76
-
77
- 2013-06-24 (v1.0.0)
78
- - save is a no-op on existing records, instead of raising an error (issue #63)
79
-
80
- 2013-06-24 (v0.10.0)
81
- - added ActiveYaml::Aliases module so you can DRY up your repetitive yaml (brett-richardson)
82
-
83
- 2013-05-23 (v0.9.14)
84
- - enum_accessor can now take multiple field names when generating the constant
85
- - temporarily disabled rails edge specs since there's an appraisal issue with minitest
86
-
87
- 2013-01-22
88
- - Fix find_by_id and find method returning nil unless .all called in ActiveYaml (mattheworiordan)
89
-
90
- 2012-07-25
91
- - Make find_by_id lookups faster by indexing records by id (desmondmonster)
92
-
93
- 2012-07-16
94
- - Validate IDs are unique by caching them in a set (desmondmonster)
95
-
96
- 2012-04-14
97
- - Support for has_one associations (kbrock)
98
-
99
- 2012-04-05
100
- - Allow gems like simple_form to read metadata about belongs_to associations that point to active hash objects (kbrock)
101
-
102
- 2012-01-19
103
- - Move specs to appraisal (flavorjones)
104
-
105
- 2012-01-18 (v0.9.8)
106
- - Make ActiveHash.find with array raise an exception when record cannot be found (mocoso)
107
-
108
- 2011-09-18 (v0.9.7)
109
- - Fixing the setting of a belongs_to_active_hash association by association (not id).
110
-
111
- 2011-08-31
112
- - 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)
113
-
114
- 2011-06-07
115
- - fixed bug where .find would not work if you defined your ids as strings
116
-
117
- 2011-06-05
118
- - fixed deprecation warnings for class_inheritable_accessor (thanks scudco!)
119
- - added basic compatibility with the `where` method from Arel (thanks rgarver!)
120
-
121
- 2011-04-19
122
- - better dependency management and compatibility with ActiveSupport 2.x (thanks vandrijevik!)
123
-
124
- 2011-01-22
125
- - improved method_missing errors for dynamic finders
126
- - prevent users from trying to overwrite :attributes (https://github.com/zilkey/active_hash/issues/#issue/33)
127
-
128
- 2010-12-08
129
- - ruby 1.9.2 compatibility
130
-
131
- 2010-12-06
132
- - added dependency on ActiveModel
133
- - add persisted? method to ActiveHash::Base
134
- - ActiveHash::Base#save takes *args to be compatible with ActiveModel
135
- - ActiveHash::Base#to_param returns nil if the object hasn't been saved
136
-
137
- 2010-11-09
138
- - Use Ruby's definition of "word character" (numbers, underscores) when forming ActiveHash::Enum constants (tstuart)
139
-
140
- 2010-11-07
141
- - Get ActiveHash::Associations to return a scope for has_many active record relationships (mocoso)
142
-
143
- 2010-10-20
144
- - Allow find_by_* methods to accept an options hash, so rails associations don't blow up
145
-
146
- 2010-10-07
147
- - Add conditions to ActiveHash#all (Ryan Garver)
148
- - Add #cache_key to ActiveHash::Base (Tom Stuart)
149
- - Add banged dynamic finder support to ActiveHash::Base (Tom Stuart)
150
-
151
- 2010-09-16
152
- - Enum format now uses underscores instead of removing all characters
153
- - Removed test dependency on acts_as_fu
154
-
155
- 2010-05-26
156
- - Silence metaclass deprecation warnings in active support 2.3.8
157
-
158
- 2010-05-04
159
- - When calling ActiveFile::Base.reload do not actually perform the reload if nothing has been modified unless you call reload(true) to force (Michael Schubert)
160
-
161
- 2010-04-25
162
- - When ActiveRecord model belongs_to an ActiveHash and the associated id is nil, returns nil instead of raising RecordNotFound (Jeremy Weiskotten)
163
- - Merged Nakajima's "add" alias for "create" - gotta save those ASCII characters :)
164
-
165
- 2010-03-01
166
- - Removed "extend"-related deprecations - they didn't play well with rails class loading
167
-
168
- 2010-01-18
169
- - Added stub for #destroyed? method, since Rails associations now depend on it
170
-
171
- 2009-12-19
172
- - Added ActiveHash::Enum (John Pignata)
173
- - Deprecated include ActiveHash::Associations in favor of extend ActiveHash::Associations
174
- - Fixed bug where you can't set nil to an association
175
- - Calling #belongs_to now creates the underlying field if it's not already there (belongs_to :city will create the :city_id field)
176
-
177
- 2009-12-10
178
- - Fixed a bug where belongs_to associations would raise an error instead of returning nil when the parent object didn't exist.
179
- - Added #[] and #[]= accessors for more ActiveRecord-esque-ness. (Pat Nakajima & Dave Yeu)
180
-
181
- 2009-12-01
182
- - Add marked_for_destruction? to be compatible with nested attributes (Brandon Keene)
183
- - Added second parameter to respond_to? and cleaned up specs (Brian Takita)
184
- - Find with an id that does not exist now raises a RecordNotFound exception to mimic ActiveRecord (Pat Nakajima)
185
-
186
- 2009-10-22
187
- - added setters to ActiveHash::Base for all fields
188
- - instantiating an ActiveHash object with a hash calls the setter methods on the object
189
- - boolean default values now work
190
-
191
- 2009-10-21
192
- - Removed auto-reloading of files based on mtime - maybe it will come back later
193
- - Made ActiveFile::Base.all a bit more sane
194
-
195
- 2009-10-13
196
- - added ActiveHash::Base.has_many, which works with ActiveRecord or ActiveHash classes (thanks to baldwindavid)
197
- - added ActiveHash::Base.belongs_to, which works with ActiveRecord or ActiveHash classes (thanks to baldwindavid)
198
- - added .delete_all method that clears the in-memory array
199
- - added support for Hash-style yaml (think, Rails fixtures)
200
- - added setter for parent object on belongs_to ( city = City.new; city.state = State.first; city.state_id == State.first.id )
201
-
202
- 2009-10-12
203
- - auto-assign fields after calling data= instead of after calling .all
204
- - remove require 'rubygems', so folks with non-gem setups can still use AH
205
- - added more specific activesupport dependency to ensure that metaclass is available
206
- - AH no longer calls to_i on ids. If you pass in a string as an id, you'll get a string back
207
- - Fancy finders, such as find_all_by_id_and_name, will compare the to_s values of the fields, so you can pass in strings
208
- - You can now use ActiveHash models as the parents of polymorphic belongs_to associations
209
- - save, save!, create and create! now add items to the in-memory collection, and naively adds autoincrementing id
210
- - new_record? returns false if the record is part of the collection
211
- - ActiveHash now works with Fixjour!
212
-
213
- 2009-08-19
214
- - Added custom finders for multiple fields, such as .find_all_by_name_and_age
215
-
216
- 2009-07-23
217
- - Added support for auto-defining methods based on hash keys in ActiveHash::Base
218
- - Changed the :field and :fields API so that they don't overwrite existing methods (useful when ActiveHash auto-defines methods)
219
- - Fixed a bug where ActiveFile incorrectly set the root_path to be the path in the gem directory, not the current working directory
220
-
221
- 2009-07-24
222
- - ActiveFile no longer reloads files by default
223
- - Added ActiveFile.reload_active_file= so you can cause ActiveFile to reload
224
- - Setting data to nil correctly causes .all to return an empty array
225
- - Added reload(force) method, so that you can force a reload from files in ActiveFile, useful for tests