property_sets 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -52,10 +52,10 @@ And for existing records:
52
52
  Using nested attributes is subject to implementing your own security measures for mass update assignments.
53
53
  Alternatively, it is possible to use a custom hash structure:
54
54
 
55
- params = { :property_sets => {
55
+ params = {
56
56
  :settings => { :version => "v4.0", :featured => "1" },
57
57
  :texts => { :epilogue => "Wibble wobble" }
58
- }}
58
+ }
59
59
  @account.update_attributes(params)
60
60
 
61
61
  The above will not update +featured+ as this has the protected flag set and is hence protected from
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,47 +1,51 @@
1
1
  require 'action_view'
2
- require 'delegate'
3
2
 
4
3
  module ActionView
5
4
  module Helpers
6
- # property_set_check_box(:account, :property_association, :property_key, options)
7
- def property_set_check_box(model_name, property_set, property, options = {}, checked_value = "1", unchecked_value = "0")
8
- the_model = @template.instance_variable_get("@#{model_name}")
9
-
10
- throw "No @#{model_name} in scope" if the_model.nil?
11
- throw "The property_set_check_box only works on models with property set #{property_set}" unless the_model.respond_to?(property_set)
12
-
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
- end
18
-
19
5
  class FormBuilder
20
- class PropertySetFormBuilderProxy < Delegator
21
- attr_accessor :builder
6
+ class PropertySetFormBuilderProxy
22
7
  attr_accessor :property_set
8
+ attr_accessor :template
9
+ attr_accessor :object_name
23
10
 
24
- def initialize(property_set, builder)
11
+ def initialize(property_set, template, object_name)
25
12
  self.property_set = property_set
26
- self.builder = builder
13
+ self.template = template
14
+ self.object_name = object_name
27
15
  end
28
16
 
29
- def __getobj__
30
- builder
17
+ def check_box(property, options = {}, checked_value = "1", unchecked_value = "0")
18
+ options = prepare_options(property, options) do |properties|
19
+ properties.send("#{property}?")
20
+ end
21
+ template.check_box(object_name, property, options, checked_value, unchecked_value)
31
22
  end
32
23
 
33
- def check_box(property, options = {}, checked_value = "1", unchecked_value = "0")
34
- builder.property_set_check_box(property_set, property, options, checked_value, unchecked_value)
24
+ def radio_button(property, checked_value = "1", options = {})
25
+ options = prepare_options(property, options) do |properties|
26
+ properties.send("#{property}") == checked_value.to_s
27
+ end
28
+ template.radio_button(object_name, property, checked_value, options)
29
+ end
30
+
31
+ def prepare_options(property, options, &block)
32
+ instance = template.instance_variable_get("@#{object_name}")
33
+ throw "No @#{object_name} in scope" if instance.nil?
34
+ throw "The property_set_check_box only works on models with property set #{property_set}" unless instance.respond_to?(property_set)
35
+
36
+ options[:id] ||= "#{object_name}_#{property_set}_#{property}"
37
+ options[:name] = "#{object_name}[#{property_set}][#{property}]"
38
+ options[:object] = instance
39
+ options[:checked] = yield(instance.send(property_set))
40
+
41
+ options
35
42
  end
36
43
  end
37
44
 
38
45
  def property_set(identifier)
39
- PropertySetFormBuilderProxy.new(identifier, self)
46
+ PropertySetFormBuilderProxy.new(identifier, @template, @object_name)
40
47
  end
41
48
 
42
- def property_set_check_box(property_set, property, options, checked_value, unchecked_value)
43
- @template.property_set_check_box(@object_name, property_set, property, objectify_options(options), checked_value, unchecked_value)
44
- end
45
49
  end
46
50
  end
47
51
  end
@@ -1,5 +1,3 @@
1
- require 'delegate'
2
-
3
1
  module PropertySets
4
2
  module ActiveRecordExtension
5
3
  module ClassMethods
@@ -7,7 +5,7 @@ module PropertySets
7
5
  unless include?(PropertySets::ActiveRecordExtension::InstanceMethods)
8
6
  self.send(:include, PropertySets::ActiveRecordExtension::InstanceMethods)
9
7
  cattr_accessor :property_set_index
10
- self.property_set_index ||= []
8
+ self.property_set_index = []
11
9
  end
12
10
 
13
11
  raise "Invalid association name, letters only" unless association.to_s =~ /[a-z]+/
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{property_sets}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.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-19}
12
+ s.date = %q{2011-01-27}
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 = [
@@ -35,7 +35,8 @@ Gem::Specification.new do |s|
35
35
  "test/fixtures/accounts.yml",
36
36
  "test/helper.rb",
37
37
  "test/schema.rb",
38
- "test/test_property_sets.rb"
38
+ "test/test_property_sets.rb",
39
+ "test/test_view_extensions.rb"
39
40
  ]
40
41
  s.homepage = %q{http://github.com/morten/property_sets}
41
42
  s.require_paths = ["lib"]
@@ -44,7 +45,8 @@ Gem::Specification.new do |s|
44
45
  s.test_files = [
45
46
  "test/helper.rb",
46
47
  "test/schema.rb",
47
- "test/test_property_sets.rb"
48
+ "test/test_property_sets.rb",
49
+ "test/test_view_extensions.rb"
48
50
  ]
49
51
 
50
52
  if s.respond_to? :specification_version then
data/test/helper.rb CHANGED
@@ -13,7 +13,7 @@ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/test.log")
13
13
  load(File.dirname(__FILE__) + "/schema.rb")
14
14
 
15
15
  $LOAD_PATH.unshift(File.dirname(__FILE__))
16
- require 'property_sets'
16
+ require File.dirname(__FILE__)+'/../lib/property_sets'
17
17
 
18
18
  class ActiveSupport::TestCase
19
19
  include ActiveRecord::TestFixtures
@@ -32,3 +32,20 @@ end
32
32
 
33
33
  ActiveSupport::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
34
34
  $LOAD_PATH.unshift(ActiveSupport::TestCase.fixture_path)
35
+
36
+ class Account < ActiveRecord::Base
37
+ property_set :settings do
38
+ property :foo
39
+ property :bar
40
+ property :baz
41
+ property :hep, :default => 'skep'
42
+ property :pro, :protected => true
43
+ end
44
+
45
+ property_set :texts do
46
+ property :foo
47
+ property :bar
48
+ end
49
+
50
+ accepts_nested_attributes_for :texts
51
+ end
@@ -1,22 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
- class Account < ActiveRecord::Base
4
- property_set :settings do
5
- property :foo
6
- property :bar
7
- property :baz
8
- property :hep, :default => 'skep'
9
- property :pro, :protected => true
10
- end
11
-
12
- property_set :texts do
13
- property :foo
14
- property :bar
15
- end
16
-
17
- accepts_nested_attributes_for :texts
18
- end
19
-
20
3
  class TestPropertySets < ActiveSupport::TestCase
21
4
 
22
5
  context "property sets" do
@@ -166,15 +149,5 @@ class TestPropertySets < ActiveSupport::TestCase
166
149
  assert !@account.settings.pro?
167
150
  end
168
151
  end
169
-
170
- context "view construction" do
171
- should "provide a form builder proxy" do
172
- proxy = ActionView::Helpers::FormBuilder.new("hi", "hi", "hi", nil, nil).property_set(:foo)
173
- assert proxy.is_a?(ActionView::Helpers::FormBuilder::PropertySetFormBuilderProxy)
174
- assert_equal :foo, proxy.property_set
175
- proxy.builder.expects(:property_set_check_box).once.with(:foo, :bar, {}, "1", "0")
176
- proxy.check_box(:bar)
177
- end
178
- end
179
152
  end
180
153
  end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ class TestViewExtensions < ActiveSupport::TestCase
4
+
5
+ context "property set view extensions" do
6
+ setup do
7
+ @association = :settings
8
+ @property = :active
9
+ @builder = ActionView::Helpers::FormBuilder.new("object_name", "object", "template", "options", "proc")
10
+ @proxy = @builder.property_set(@association)
11
+ end
12
+
13
+ should "provide a form builder proxy" do
14
+ assert @proxy.is_a?(ActionView::Helpers::FormBuilder::PropertySetFormBuilderProxy)
15
+ assert_equal @association, @proxy.property_set
16
+ end
17
+
18
+ context "check_box" do
19
+ should "call with checked true for a truth value" do
20
+ settings = stub(@property => "1", "#{@property}?".to_sym => true)
21
+ object = stub()
22
+ object.expects(@association).returns(settings)
23
+ options = {
24
+ :checked => true, :name => "object_name[#{@association}][#{@property}]",
25
+ :id => "object_name_#{@association}_#{@property}", :object => object
26
+ }
27
+ template = stub()
28
+ template.expects(:instance_variable_get).with("@object_name").returns(object)
29
+ # def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
30
+ template.expects(:check_box).with("object_name", @property, options, "1", "0")
31
+
32
+ @proxy.stubs(:template).returns(template)
33
+ @proxy.check_box(@property)
34
+ end
35
+
36
+ should "call with checked false for a truth value" do
37
+ settings = stub(@property => "0", "#{@property}?".to_sym => false)
38
+ object = stub()
39
+ object.expects(@association).returns(settings)
40
+ options = {
41
+ :checked => false, :name => "object_name[#{@association}][#{@property}]",
42
+ :id => "object_name_#{@association}_#{@property}", :object => object
43
+ }
44
+ template = stub()
45
+ template.expects(:instance_variable_get).with("@object_name").returns(object)
46
+ # def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
47
+ template.expects(:check_box).with("object_name", @property, options, "1", "0")
48
+
49
+ @proxy.stubs(:template).returns(template)
50
+ @proxy.check_box(@property)
51
+ end
52
+ end
53
+
54
+ context "radio_button" do
55
+ should "call with checked true for a truth value" do
56
+ settings = stub(@property => "hello")
57
+ object = stub()
58
+ object.expects(@association).returns(settings)
59
+ options = {
60
+ :checked => true, :name => "object_name[#{@association}][#{@property}]",
61
+ :id => "object_name_#{@association}_#{@property}", :object => object
62
+ }
63
+ template = stub()
64
+ template.expects(:instance_variable_get).with("@object_name").returns(object)
65
+ # def radio_button(object_name, method, tag_value, options = {})
66
+ template.expects(:radio_button).with("object_name", @property, "hello", options)
67
+
68
+ @proxy.stubs(:template).returns(template)
69
+ @proxy.radio_button(@property, "hello")
70
+ end
71
+ end
72
+ end
73
+ end
74
+
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: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.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-19 00:00:00 -08:00
18
+ date: 2011-01-27 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -169,6 +169,7 @@ files:
169
169
  - test/helper.rb
170
170
  - test/schema.rb
171
171
  - test/test_property_sets.rb
172
+ - test/test_view_extensions.rb
172
173
  has_rdoc: true
173
174
  homepage: http://github.com/morten/property_sets
174
175
  licenses: []
@@ -207,3 +208,4 @@ test_files:
207
208
  - test/helper.rb
208
209
  - test/schema.rb
209
210
  - test/test_property_sets.rb
211
+ - test/test_view_extensions.rb