active_hash 3.0.0 → 3.2.1

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