johnsbrn-classy-inheritance 0.6.3.2 → 0.6.4
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/History.txt +3 -0
- data/lib/classy-inheritance.rb +9 -4
- data/test/test_helper.rb +26 -0
- metadata +4 -2
data/History.txt
CHANGED
data/lib/classy-inheritance.rb
CHANGED
@@ -40,7 +40,10 @@ module Stonean
|
|
40
40
|
options[:attrs].each{|attr| define_accessors(model_sym, attr, options)}
|
41
41
|
end
|
42
42
|
|
43
|
-
|
43
|
+
def has_dependency(model_sym, options = {})
|
44
|
+
depends_on(model_sym, options.update(:has_dependency => true))
|
45
|
+
end
|
46
|
+
|
44
47
|
def can_be(model_sym, options = {})
|
45
48
|
unless options[:as]
|
46
49
|
raise ArgumentError, ":as attribute required when calling can_be"
|
@@ -62,7 +65,7 @@ module Stonean
|
|
62
65
|
private
|
63
66
|
|
64
67
|
def classy_options
|
65
|
-
[:as, :attrs, :prefix, :postfix, :validates_presence_if, :validates_associated_if]
|
68
|
+
[:as, :attrs, :has_dependency, :prefix, :postfix, :validates_presence_if, :validates_associated_if]
|
66
69
|
end
|
67
70
|
|
68
71
|
def delete_classy_options(options, *keepers)
|
@@ -78,6 +81,8 @@ module Stonean
|
|
78
81
|
as_opt = opts.delete(:as)
|
79
82
|
opts = polymorphic_constraints(as_opt).merge(opts)
|
80
83
|
has_one model_sym, opts
|
84
|
+
elsif options[:has_dependency]
|
85
|
+
has_one model_sym, opts
|
81
86
|
else
|
82
87
|
belongs_to model_sym, opts
|
83
88
|
end
|
@@ -138,7 +143,7 @@ module Stonean
|
|
138
143
|
if options[:postfix]
|
139
144
|
accessor_method_name = (options[:postfix] == true) ? "#{accessor_method_name}_#{model_sym}" : "#{accessor_method_name}_#{options[:postfix]}"
|
140
145
|
end
|
141
|
-
|
146
|
+
|
142
147
|
define_method accessor_method_name do
|
143
148
|
eval("self.#{model_sym} ? self.#{model_sym}.#{attr} : nil")
|
144
149
|
end
|
@@ -176,7 +181,7 @@ if Object.const_defined?("ActiveRecord") && ActiveRecord.const_defined?("Base")
|
|
176
181
|
module ActiveRecord::Validations::ClassMethods
|
177
182
|
|
178
183
|
def validates_associated_dependent(model_sym, options, configuration = {})
|
179
|
-
configuration = { :message =>
|
184
|
+
configuration = { :message => I18n.translate('activerecord.errors.messages')[:invalid], :on => :save }.update(configuration)
|
180
185
|
|
181
186
|
validates_each(model_sym, configuration) do |record, attr_name, value|
|
182
187
|
associate = record.send(attr_name)
|
data/test/test_helper.rb
CHANGED
@@ -19,6 +19,22 @@ class SetupTestTables < ActiveRecord::Migration
|
|
19
19
|
t.timestamps
|
20
20
|
end
|
21
21
|
|
22
|
+
create_table :accounts, :force => true do |t|
|
23
|
+
t.string :first_name
|
24
|
+
t.string :last_name
|
25
|
+
t.string :email
|
26
|
+
|
27
|
+
t.timestamps
|
28
|
+
end
|
29
|
+
|
30
|
+
create_table :account_logins, :force => true do |t|
|
31
|
+
t.string :login
|
32
|
+
t.string :password
|
33
|
+
t.integer :account_id
|
34
|
+
|
35
|
+
t.timestamps
|
36
|
+
end
|
37
|
+
|
22
38
|
create_table :users, :force => true do |t|
|
23
39
|
t.string :login
|
24
40
|
t.integer :profile_id
|
@@ -82,6 +98,8 @@ class SetupTestTables < ActiveRecord::Migration
|
|
82
98
|
end
|
83
99
|
|
84
100
|
def self.down
|
101
|
+
drop_table :accounts
|
102
|
+
drop_table :account_logins
|
85
103
|
drop_table :authors
|
86
104
|
drop_table :artists
|
87
105
|
drop_table :users
|
@@ -100,6 +118,14 @@ class Profile < ActiveRecord::Base
|
|
100
118
|
validates_presence_of :first_name, :last_name, :email
|
101
119
|
end
|
102
120
|
|
121
|
+
class Account < ActiveRecord::Base
|
122
|
+
validates_presence_of :first_name, :last_name, :email
|
123
|
+
end
|
124
|
+
|
125
|
+
class AccountLogin < ActiveRecord::Base
|
126
|
+
validates_presence_of :login, :password
|
127
|
+
end
|
128
|
+
|
103
129
|
class User < ActiveRecord::Base
|
104
130
|
validates_presence_of :login
|
105
131
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: johnsbrn-classy-inheritance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Stone
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-01
|
12
|
+
date: 2009-02-01 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- test/test_with_optional_dependency.rb
|
45
45
|
- test/test_with_prefix_postfix.rb
|
46
46
|
- test/test_with_standard_attributes.rb
|
47
|
+
- test/test_has_dependency.rb
|
47
48
|
has_rdoc: true
|
48
49
|
homepage: http://stonean.com/wiki/classy-inheritance
|
49
50
|
post_install_message:
|
@@ -73,6 +74,7 @@ specification_version: 2
|
|
73
74
|
summary: Classy Inheritance adds a depends_on class method to your ActiveRecord model so that you can define requisite objects
|
74
75
|
test_files:
|
75
76
|
- test/test_classy-inheritance.rb
|
77
|
+
- test/test_has_dependency.rb
|
76
78
|
- test/test_helper.rb
|
77
79
|
- test/test_polymorphic_associations.rb
|
78
80
|
- test/test_with_optional_dependency.rb
|