active_hash 0.7.7 → 0.7.8
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 +5 -0
- data/VERSION +1 -1
- data/active_hash.gemspec +2 -2
- data/geminstaller.yml +4 -4
- data/lib/active_hash/base.rb +4 -0
- data/lib/associations/associations.rb +3 -1
- data/lib/enum/enum.rb +19 -13
- data/spec/associations/associations_spec.rb +11 -3
- data/spec/enum/enum_spec.rb +34 -3
- data/spec/fixtures/boroughs.yml +0 -4
- metadata +2 -2
data/CHANGELOG
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
2010-01-18
|
|
2
|
+
- Added stub for #destroyed? method, since Rails associations now depend on it
|
|
3
|
+
|
|
1
4
|
2009-12-19
|
|
2
5
|
- Added ActiveHash::Enum (John Pignata)
|
|
3
6
|
- Deprecated include ActiveHash::Associations in favor of extend ActiveHash::Associations
|
|
7
|
+
- Fixed bug where you can't set nil to an association
|
|
8
|
+
- Calling #belongs_to now creates the underlying field if it's not already there (belongs_to :city will create the :city_id field)
|
|
4
9
|
|
|
5
10
|
2009-12-10
|
|
6
11
|
- Fixed a bug where belongs_to associations would raise an error instead of returning
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.7.
|
|
1
|
+
0.7.8
|
data/active_hash.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{active_hash}
|
|
8
|
-
s.version = "0.7.
|
|
8
|
+
s.version = "0.7.8"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Jeff Dean", "Mike Dalessio", "Corey Innis", "Peter Jaros", "Brandon Keene", "Brian Takita", "Pat Nakajima", "John Pignata"]
|
|
12
|
-
s.date = %q{
|
|
12
|
+
s.date = %q{2010-01-18}
|
|
13
13
|
s.email = %q{jeff@zilkey.com}
|
|
14
14
|
s.extra_rdoc_files = [
|
|
15
15
|
"LICENSE",
|
data/geminstaller.yml
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
defaults:
|
|
2
|
-
install_options: --source=http://gemcutter.org --
|
|
2
|
+
install_options: --source=http://gemcutter.org --include-dependencies --no-ri --no-rdoc
|
|
3
3
|
gems:
|
|
4
4
|
- name: acts_as_fu
|
|
5
5
|
version: 0.0.5
|
|
6
6
|
- name: activesupport
|
|
7
7
|
version: >= 2.2.2
|
|
8
8
|
- name: rspec
|
|
9
|
-
version: >= 1.
|
|
9
|
+
version: >= 1.3.0
|
|
10
10
|
- name: fixjour
|
|
11
|
-
version: 0.
|
|
11
|
+
version: 0.3.0
|
|
12
12
|
- name: jeweler
|
|
13
|
-
version: 1.
|
|
13
|
+
version: 1.4.0
|
data/lib/active_hash/base.rb
CHANGED
|
@@ -31,12 +31,14 @@ module ActiveHash
|
|
|
31
31
|
:foreign_key => association_id.to_s.foreign_key
|
|
32
32
|
}.merge(options)
|
|
33
33
|
|
|
34
|
+
field options[:foreign_key].to_sym
|
|
35
|
+
|
|
34
36
|
define_method(association_id) do
|
|
35
37
|
options[:class_name].constantize.find_by_id(send(options[:foreign_key]))
|
|
36
38
|
end
|
|
37
39
|
|
|
38
40
|
define_method("#{association_id}=") do |new_value|
|
|
39
|
-
attributes[ options[:foreign_key].to_sym ] = new_value.id
|
|
41
|
+
attributes[ options[:foreign_key].to_sym ] = new_value ? new_value.id : nil
|
|
40
42
|
end
|
|
41
43
|
|
|
42
44
|
end
|
data/lib/enum/enum.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module ActiveHash
|
|
2
2
|
module Enum
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
DuplicateEnumAccessor = Class.new(RuntimeError)
|
|
5
5
|
|
|
6
6
|
def enum_accessor(field_name)
|
|
7
7
|
@enum_accessor = field_name
|
|
@@ -10,30 +10,36 @@ module ActiveHash
|
|
|
10
10
|
|
|
11
11
|
def insert(record)
|
|
12
12
|
super
|
|
13
|
-
set_constant(record) if enum_accessor?
|
|
13
|
+
set_constant(record) if @enum_accessor.present?
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
def delete_all
|
|
17
|
+
if @enum_accessor.present?
|
|
18
|
+
@records.each do |record|
|
|
19
|
+
constant = constant_for(record.attributes[@enum_accessor])
|
|
20
|
+
remove_const(constant) if const_defined?(constant)
|
|
21
|
+
end
|
|
21
22
|
end
|
|
23
|
+
super
|
|
22
24
|
end
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
def set_constant(record)
|
|
27
|
+
constant = constant_for(record.attributes[@enum_accessor])
|
|
28
|
+
return nil if constant.blank?
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
unless const_defined?(constant)
|
|
31
|
+
const_set(constant, record)
|
|
32
|
+
else
|
|
33
|
+
raise DuplicateEnumAccessor, "#{constant} already defined for #{self.class}" unless const_get(constant) == record
|
|
34
|
+
end
|
|
28
35
|
end
|
|
29
36
|
|
|
30
|
-
private :
|
|
37
|
+
private :set_constant
|
|
31
38
|
|
|
32
39
|
def constant_for(field_value)
|
|
33
|
-
if constant = field_value.dup
|
|
40
|
+
if constant = field_value.try(:dup)
|
|
34
41
|
constant.gsub!(/[^A-Za-z]*/, "")
|
|
35
42
|
constant.upcase!
|
|
36
|
-
raise DuplicateConstant, "#{constant} is already defined on #{self.class.name}" if const_defined?(constant)
|
|
37
43
|
constant
|
|
38
44
|
end
|
|
39
45
|
end
|
|
@@ -37,14 +37,11 @@ describe ActiveHash::Base, "associations" do
|
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
class City < ActiveHash::Base
|
|
40
|
-
field :country_id
|
|
41
40
|
extend ActiveHash::Associations
|
|
42
41
|
end
|
|
43
42
|
|
|
44
43
|
class Author < ActiveHash::Base
|
|
45
44
|
extend ActiveHash::Associations
|
|
46
|
-
field :publisher_id
|
|
47
|
-
field :city_id
|
|
48
45
|
end
|
|
49
46
|
|
|
50
47
|
build_model :books do
|
|
@@ -75,6 +72,7 @@ describe ActiveHash::Base, "associations" do
|
|
|
75
72
|
|
|
76
73
|
context "with ActiveHash children" do
|
|
77
74
|
before do
|
|
75
|
+
Author.field :city_id
|
|
78
76
|
@included_author_1 = Author.create :city_id => 1
|
|
79
77
|
@included_author_2 = Author.create :city_id => 1
|
|
80
78
|
@excluded_author = Author.create :city_id => 2
|
|
@@ -144,6 +142,16 @@ describe ActiveHash::Base, "associations" do
|
|
|
144
142
|
author.city_id.should == @city.id
|
|
145
143
|
author.city.should == @city
|
|
146
144
|
end
|
|
145
|
+
|
|
146
|
+
it "works with nil" do
|
|
147
|
+
author = Author.new :city => @city
|
|
148
|
+
author.city_id.should == @city.id
|
|
149
|
+
author.city.should == @city
|
|
150
|
+
|
|
151
|
+
author.city = nil
|
|
152
|
+
author.city_id.should be_nil
|
|
153
|
+
author.city.should be_nil
|
|
154
|
+
end
|
|
147
155
|
end
|
|
148
156
|
|
|
149
157
|
describe "with a different foreign key" do
|
data/spec/enum/enum_spec.rb
CHANGED
|
@@ -8,6 +8,7 @@ describe ActiveHash::Base, "enum" do
|
|
|
8
8
|
class Borough < ActiveYaml::Base
|
|
9
9
|
extend ActiveHash::Enum
|
|
10
10
|
fields :name, :county, :population
|
|
11
|
+
enum_accessor :name
|
|
11
12
|
end
|
|
12
13
|
end
|
|
13
14
|
|
|
@@ -17,10 +18,9 @@ describe ActiveHash::Base, "enum" do
|
|
|
17
18
|
|
|
18
19
|
describe "#enum_accessor" do
|
|
19
20
|
it "sets the field used for accessing records by constants" do
|
|
20
|
-
Borough.enum_accessor :name
|
|
21
21
|
Borough::BROOKLYN.should == Borough.find_by_name("Brooklyn")
|
|
22
22
|
end
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
it "ensures that values stored in the field specified are unique" do
|
|
25
25
|
lambda do
|
|
26
26
|
Class.new(ActiveHash::Base) do
|
|
@@ -32,7 +32,7 @@ describe ActiveHash::Base, "enum" do
|
|
|
32
32
|
]
|
|
33
33
|
enum_accessor :name
|
|
34
34
|
end
|
|
35
|
-
end.should raise_error(ActiveHash::Enum::
|
|
35
|
+
end.should raise_error(ActiveHash::Enum::DuplicateEnumAccessor)
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it "removes non-word characters from values before setting constants" do
|
|
@@ -52,4 +52,35 @@ describe ActiveHash::Base, "enum" do
|
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
+
context "ActiveHash with an enum_accessor set" do
|
|
56
|
+
describe "#save" do
|
|
57
|
+
it "resets the constant's value to the updated record" do
|
|
58
|
+
Borough::BROOKLYN.population.should == 2556598
|
|
59
|
+
brooklyn = Borough.find_by_name("Brooklyn")
|
|
60
|
+
brooklyn.population = 2556600
|
|
61
|
+
brooklyn.save.should be_true
|
|
62
|
+
Borough::BROOKLYN.population.should == 2556600
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
describe ".create" do
|
|
67
|
+
it "creates constants for new records" do
|
|
68
|
+
bronx = Borough.create!(:name => "Bronx")
|
|
69
|
+
Borough::BRONX.should == bronx
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "doesn't create constants for records missing the enum accessor field" do
|
|
73
|
+
Borough.create(:name => "").should be_true
|
|
74
|
+
Borough.create(:population => 12).should be_true
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
describe ".delete_all" do
|
|
79
|
+
it "unsets all constants for deleted records" do
|
|
80
|
+
Borough.const_defined?("STATENISLAND").should be_true
|
|
81
|
+
Borough.delete_all.should be_true
|
|
82
|
+
Borough.const_defined?("STATENISLAND").should be_false
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
55
86
|
end
|
data/spec/fixtures/boroughs.yml
CHANGED
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.7.
|
|
4
|
+
version: 0.7.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jeff Dean
|
|
@@ -16,7 +16,7 @@ autorequire:
|
|
|
16
16
|
bindir: bin
|
|
17
17
|
cert_chain: []
|
|
18
18
|
|
|
19
|
-
date:
|
|
19
|
+
date: 2010-01-18 00:00:00 -05:00
|
|
20
20
|
default_executable:
|
|
21
21
|
dependencies:
|
|
22
22
|
- !ruby/object:Gem::Dependency
|