property_sets 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -10,10 +10,10 @@ module ActionView
|
|
10
10
|
throw "No @#{model_name} in scope" if the_model.nil?
|
11
11
|
throw "The property_set_check_box only works on models with property set #{property_set}" unless the_model.respond_to?(property_set)
|
12
12
|
|
13
|
-
options[:checked] = the_model.send(
|
14
|
-
options[:id] ||= "#{model_name}
|
15
|
-
options[:name] = "#{model_name}[
|
16
|
-
@template.check_box(model_name, "
|
13
|
+
options[:checked] = the_model.send(property_set).send("#{property}?")
|
14
|
+
options[:id] ||= "#{model_name}_#{property_set}_#{property}"
|
15
|
+
options[:name] = "#{model_name}[#{property_set}][#{property}]"
|
16
|
+
@template.check_box(model_name, "#{property_set}_#{property}", options, checked_value, unchecked_value)
|
17
17
|
end
|
18
18
|
|
19
19
|
class FormBuilder
|
@@ -2,10 +2,17 @@ require 'delegate'
|
|
2
2
|
|
3
3
|
module PropertySets
|
4
4
|
module ActiveRecordExtension
|
5
|
-
|
6
5
|
module ClassMethods
|
7
6
|
def property_set(association, &block)
|
7
|
+
unless include?(PropertySets::ActiveRecordExtension::InstanceMethods)
|
8
|
+
self.send(:include, PropertySets::ActiveRecordExtension::InstanceMethods)
|
9
|
+
cattr_accessor :property_set_index
|
10
|
+
self.property_set_index ||= []
|
11
|
+
end
|
12
|
+
|
8
13
|
raise "Invalid association name, letters only" unless association.to_s =~ /[a-z]+/
|
14
|
+
self.property_set_index << association
|
15
|
+
|
9
16
|
property_class = PropertySets.ensure_property_set_class(association, self)
|
10
17
|
property_class.instance_eval(&block)
|
11
18
|
|
@@ -59,6 +66,11 @@ module PropertySets
|
|
59
66
|
end
|
60
67
|
|
61
68
|
module InstanceMethods
|
69
|
+
def self.included(base)
|
70
|
+
base.alias_method_chain :update_attributes, :property_sets
|
71
|
+
base.alias_method_chain :update_attributes!, :property_sets
|
72
|
+
end
|
73
|
+
|
62
74
|
def update_attributes_with_property_sets(attributes)
|
63
75
|
update_property_set_attributes(attributes)
|
64
76
|
update_attributes_without_property_sets(attributes)
|
@@ -70,20 +82,17 @@ module PropertySets
|
|
70
82
|
end
|
71
83
|
|
72
84
|
def update_property_set_attributes(attributes)
|
73
|
-
if attributes &&
|
74
|
-
|
75
|
-
|
85
|
+
if attributes && self.class.property_set_index.any?
|
86
|
+
self.class.property_set_index.each do |property_set|
|
87
|
+
if property_set_hash = attributes.delete(property_set)
|
88
|
+
send(property_set).set(property_set_hash, true)
|
89
|
+
end
|
76
90
|
end
|
77
91
|
end
|
78
92
|
end
|
79
93
|
end
|
94
|
+
|
80
95
|
end
|
81
96
|
end
|
82
97
|
|
83
|
-
ActiveRecord::Base.
|
84
|
-
include PropertySets::ActiveRecordExtension::InstanceMethods
|
85
|
-
extend PropertySets::ActiveRecordExtension::ClassMethods
|
86
|
-
|
87
|
-
alias_method_chain :update_attributes, :property_sets
|
88
|
-
alias_method_chain :update_attributes!, :property_sets
|
89
|
-
end
|
98
|
+
ActiveRecord::Base.extend PropertySets::ActiveRecordExtension::ClassMethods
|
data/property_sets.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{property_sets}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Morten Primdahl"]
|
12
|
-
s.date = %q{2011-01-
|
12
|
+
s.date = %q{2011-01-19}
|
13
13
|
s.description = %q{This gem is an ActiveRecord extension which provides a convenient interface for managing per row properties}
|
14
14
|
s.email = %q{morten@zendesk.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/test/test_property_sets.rb
CHANGED
@@ -31,6 +31,10 @@ class TestPropertySets < ActiveSupport::TestCase
|
|
31
31
|
assert defined?(AccountText)
|
32
32
|
end
|
33
33
|
|
34
|
+
should "register the property sets used on a class" do
|
35
|
+
assert_equal [ :settings, :texts ], Account.property_set_index
|
36
|
+
end
|
37
|
+
|
34
38
|
should "support protecting attributes" do
|
35
39
|
assert @account.settings.protected?(:pro)
|
36
40
|
assert !@account.settings.protected?(:foo)
|
@@ -138,10 +142,7 @@ class TestPropertySets < ActiveSupport::TestCase
|
|
138
142
|
|
139
143
|
should "be updateable as a nested structure" do
|
140
144
|
attribs = {
|
141
|
-
:name => "Kim",
|
142
|
-
:property_sets => {
|
143
|
-
:settings => { :foo => "1", :bar => "0" }
|
144
|
-
}
|
145
|
+
:name => "Kim", :settings => { :foo => "1", :bar => "0" }
|
145
146
|
}
|
146
147
|
|
147
148
|
assert @account.update_attributes(attribs)
|
@@ -154,10 +155,7 @@ class TestPropertySets < ActiveSupport::TestCase
|
|
154
155
|
assert !@account.settings.pro?
|
155
156
|
|
156
157
|
attribs = {
|
157
|
-
:name => "Kim",
|
158
|
-
:property_sets => {
|
159
|
-
:settings => { :foo => "1", :bar => "1", :baz => "1", :pro => "1" }
|
160
|
-
}
|
158
|
+
:name => "Kim", :settings => { :foo => "1", :bar => "1", :baz => "1", :pro => "1" }
|
161
159
|
}
|
162
160
|
|
163
161
|
assert @account.update_attributes!(attribs)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: property_sets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 1
|
9
8
|
- 2
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Morten Primdahl
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-19 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|