active_hash 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/CHANGELOG +3 -0
- data/README.md +29 -6
- data/lib/active_hash/version.rb +1 -1
- data/lib/active_yaml/base.rb +2 -0
- data/lib/associations/associations.rb +28 -12
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
YzA5OWQ3YjMzNGQ5MzM0NzhkZjk3ZDJmODFiMDdkMzA0YTRiODc5OA==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 07f278485648e475f69c32f40ae5f647a7e275c5
|
4
|
+
data.tar.gz: 04811d2113fdf923916200ebfe3966f6f6d3e4be
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MTk3NWE4MGIyYTc1ZDhjYzYxYTc0MDkyYWU5MTE4NTg2ODhiZGE5NjhlYWM1
|
11
|
-
YWQ4OTJmYWJlOWUyZGE4NzMwZTkwNTdhMGJlYmFjOTljYzI2Njk=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
YzAzMzJlMTYzNmVlYzA2NDBjYmMxZTViOWNjNmMzNDNlNGFjMTUwZGUzYTk4
|
14
|
-
NDViYjk2MTUzYzZmZTNjOGNlNTUwZDM4NDA5ZjE5MzM0NTkxN2ZhNTljMjc1
|
15
|
-
ZGFjYWRkODJiMjNiMGJiN2Q1Njc2MGExNmE0MWJhMDUxOTdhOWI=
|
6
|
+
metadata.gz: 76e9b4d4d67e4863473388961024dd66af1b2b81771798f1fb24417060ffd93b93887dadea24dce45cde7d13ccf0759c93775710987281ed18a05009bb083bb1
|
7
|
+
data.tar.gz: c29c149a4db09945d9c616d616223b629771d74316d43465d6b412b7b02b0146fe09f37270734579bc11b77b71315cde671387ea1cace714f39eb9c1bff92b8c
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
2014-09-03 (v1.4.0)
|
2
|
+
- support Rails 4.2 (agraves, al2o3cr)
|
3
|
+
|
1
4
|
2014-02-18 (v1.3.0)
|
2
5
|
- fix bug where including ActiveHash associations would make `belongs_to :imageable, polymorphic: true` blow up
|
3
6
|
- fixed several bugs that prevented active hash from being used without active record / active model
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
ActiveHash is a simple base class that allows you to use a ruby hash as a readonly datasource for an ActiveRecord-like model.
|
6
6
|
|
7
|
-
ActiveHash assumes that every hash has an :id key, which is what you would probably store in a database. This allows you to
|
7
|
+
ActiveHash assumes that every hash has an :id key, which is what you would probably store in a database. This allows you to seamlessly upgrade from ActiveHash objects to full ActiveRecord objects without having to change any code in your app, or any foreign keys in your database.
|
8
8
|
|
9
9
|
It also allows you to use #has_many and #belongs_to (via belongs_to_active_hash) in your AR objects.
|
10
10
|
|
@@ -23,7 +23,34 @@ Bundler:
|
|
23
23
|
|
24
24
|
Other:
|
25
25
|
|
26
|
-
|
26
|
+
gem install active_hash
|
27
|
+
|
28
|
+
## Reason for being
|
29
|
+
|
30
|
+
We wrote ActiveHash so that we could use simple, in-memory, ActiveRecord-like data structures that play well with Rails forms, like:
|
31
|
+
|
32
|
+
# in app/models/country.rb
|
33
|
+
class Country < ActiveHash::Base
|
34
|
+
self.data = [
|
35
|
+
{:id => 1, :name => "US"},
|
36
|
+
{:id => 2, :name => "Canada"}
|
37
|
+
]
|
38
|
+
end
|
39
|
+
|
40
|
+
# in some view
|
41
|
+
<%= collection_select :person, :country_id, Country.all, :id, :name %>
|
42
|
+
|
43
|
+
Before ActiveHash, we did things like:
|
44
|
+
|
45
|
+
# in app/models/person.rb
|
46
|
+
class Person < ActiveRecord::Base
|
47
|
+
COUNTRIES = ["US", "Canada"]
|
48
|
+
end
|
49
|
+
|
50
|
+
# in some view
|
51
|
+
<%= collection_select :person, :country_id, Person::COUNTRIES, :to_s, :to_s %>
|
52
|
+
|
53
|
+
The majority of ActiveHash uses involve setting up some data at boot time, and never modifying that data at runtime.
|
27
54
|
|
28
55
|
## Usage
|
29
56
|
|
@@ -229,10 +256,6 @@ With ActiveRecord versions < 3.1, ActiveHash will also work as a polymorphic par
|
|
229
256
|
|
230
257
|
However, as of ActiveRecord 3.1 this will not work. If you need support for that, please open an issue.
|
231
258
|
|
232
|
-
You can also use standard rails view helpers, like #collection_select:
|
233
|
-
|
234
|
-
<%= collection_select :person, :country_id, Country.all, :id, :name %>
|
235
|
-
|
236
259
|
### Using shortcuts
|
237
260
|
|
238
261
|
Since ActiveHashes usually are static, we can use shortcuts to assign via an easy to remember string instead of an obscure ID number.
|
data/lib/active_hash/version.rb
CHANGED
data/lib/active_yaml/base.rb
CHANGED
@@ -44,25 +44,40 @@ module ActiveHash
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
create_reflection(
|
47
|
+
if ActiveRecord::Reflection.respond_to?(:create)
|
48
|
+
reflection = ActiveRecord::Reflection.create(
|
50
49
|
:belongs_to,
|
51
50
|
association_id.to_sym,
|
52
51
|
nil,
|
53
52
|
options,
|
54
53
|
self
|
55
54
|
)
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
|
56
|
+
ActiveRecord::Reflection.add_reflection(
|
57
|
+
self,
|
59
58
|
association_id.to_sym,
|
60
|
-
|
61
|
-
options[:class_name].constantize
|
59
|
+
reflection
|
62
60
|
)
|
61
|
+
else
|
62
|
+
method = ActiveRecord::Base.method(:create_reflection)
|
63
|
+
if method.respond_to?(:parameters) && method.parameters.length == 5
|
64
|
+
create_reflection(
|
65
|
+
:belongs_to,
|
66
|
+
association_id.to_sym,
|
67
|
+
nil,
|
68
|
+
options,
|
69
|
+
self
|
70
|
+
)
|
71
|
+
else
|
72
|
+
create_reflection(
|
73
|
+
:belongs_to,
|
74
|
+
association_id.to_sym,
|
75
|
+
options,
|
76
|
+
options[:class_name].constantize
|
77
|
+
)
|
78
|
+
end
|
63
79
|
end
|
64
80
|
end
|
65
|
-
|
66
81
|
end
|
67
82
|
|
68
83
|
def self.included(base)
|
@@ -81,13 +96,14 @@ module ActiveHash
|
|
81
96
|
|
82
97
|
klass = options[:class_name].constantize
|
83
98
|
primary_key_value = send(options[:primary_key])
|
99
|
+
foreign_key = options[:foreign_key].to_sym
|
84
100
|
|
85
101
|
if Object.const_defined?(:ActiveRecord) && ActiveRecord.const_defined?(:Relation) && klass < ActiveRecord::Relation
|
86
|
-
klass.where(
|
102
|
+
klass.where(foreign_key => primary_key_value)
|
87
103
|
elsif klass.respond_to?(:scoped)
|
88
|
-
klass.scoped(:conditions => {
|
104
|
+
klass.scoped(:conditions => {foreign_key => primary_key_value})
|
89
105
|
else
|
90
|
-
klass.
|
106
|
+
klass.where(foreign_key => primary_key_value)
|
91
107
|
end
|
92
108
|
end
|
93
109
|
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: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Dean
|
@@ -28,20 +28,20 @@ authors:
|
|
28
28
|
autorequire:
|
29
29
|
bindir: bin
|
30
30
|
cert_chain: []
|
31
|
-
date: 2014-
|
31
|
+
date: 2014-09-04 00:00:00.000000000 Z
|
32
32
|
dependencies:
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: activesupport
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- -
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: 2.2.2
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: 2.2.2
|
47
47
|
description: Includes the ability to specify data using hashes, yml files or JSON
|
@@ -76,17 +76,17 @@ require_paths:
|
|
76
76
|
- lib
|
77
77
|
required_ruby_version: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- -
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
83
|
requirements:
|
84
|
-
- -
|
84
|
+
- - ">="
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project:
|
89
|
-
rubygems_version: 2.2.
|
89
|
+
rubygems_version: 2.2.2
|
90
90
|
signing_key:
|
91
91
|
specification_version: 4
|
92
92
|
summary: An ActiveRecord-like model that uses a hash or file as a datasource
|