property_sets 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.5.2
@@ -28,6 +28,10 @@ module ActionView
28
28
  template.radio_button(object_name, property, checked_value, options)
29
29
  end
30
30
 
31
+ def text_field(property, options = {})
32
+ template.text_field(object_name, property, prepare_id_name(property, options))
33
+ end
34
+
31
35
  def hidden_field(property, options = {})
32
36
  template.hidden_field(object_name, property, prepare_id_name(property, options))
33
37
  end
@@ -28,6 +28,35 @@ module PropertySets
28
28
  end
29
29
  end
30
30
 
31
+ def read_value_cast_for_property_set(type, value)
32
+ return nil if value.nil?
33
+
34
+ case type
35
+ when :string
36
+ value
37
+ when :datetime
38
+ Time.parse(value).in_time_zone
39
+ when :float
40
+ value.to_f
41
+ when :integer
42
+ value.to_i
43
+ end
44
+ end
45
+
46
+ def write_value_cast_for_property_set(type, value)
47
+ return nil if value.nil?
48
+ case type
49
+ when :datetime
50
+ if value.is_a?(String)
51
+ value
52
+ else
53
+ value.in_time_zone("UTC").to_s
54
+ end
55
+ else
56
+ value.to_s
57
+ end
58
+ end
59
+
31
60
  property_class.keys.each do |key|
32
61
  raise "Invalid property key #{key}" if self.respond_to?(key)
33
62
 
@@ -38,7 +67,13 @@ module PropertySets
38
67
 
39
68
  # Returns the value of the property
40
69
  define_method "#{key}" do
41
- lookup(key).value
70
+ read_value_cast_for_property_set(property_class.type(key), lookup(key).value)
71
+ end
72
+
73
+ # Assigns a new value to the property
74
+ define_method "#{key}=" do |value|
75
+ instance = lookup(key)
76
+ instance.value = write_value_cast_for_property_set(property_class.type(key), value)
42
77
  end
43
78
 
44
79
  define_method "#{key}_record" do
@@ -57,13 +92,6 @@ module PropertySets
57
92
  send("#{arg}=", "0")
58
93
  end
59
94
 
60
- # Assigns a new value to the property
61
- define_method "#{key}=" do |value|
62
- instance = lookup(key)
63
- instance.value = value
64
- value
65
- end
66
-
67
95
  # The finder method which returns the property if present, otherwise a new instance with defaults
68
96
  define_method "lookup" do |arg|
69
97
  instance = detect { |property| property.name.to_sym == arg.to_sym }
@@ -81,7 +109,6 @@ module PropertySets
81
109
  instance = detect { |property| property.name.to_sym == arg.to_sym }
82
110
  instance ||= property_class.new(:value => property_class.default(arg))
83
111
  end
84
-
85
112
  end
86
113
  end
87
114
  end
@@ -42,6 +42,7 @@ module PropertySets
42
42
  self.value = value.to_s unless value.nil?
43
43
  end
44
44
 
45
+
45
46
  def owner_class_instance
46
47
  send(self.class.owner_class_sym)
47
48
  end
@@ -66,6 +67,10 @@ module PropertySets
66
67
  @properties[key] && @properties[key].key?(:default) ? @properties[key][:default] : nil
67
68
  end
68
69
 
70
+ def type(key)
71
+ @properties[key] && @properties[key].key?(:type) ? @properties[key][:type] : :string
72
+ end
73
+
69
74
  def protected?(key)
70
75
  @properties[key] && !!@properties[key][:protected]
71
76
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{property_sets}
8
- s.version = "0.5.1"
8
+ s.version = "0.5.2"
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-04-26}
12
+ s.date = %q{2011-08-03}
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 = [
@@ -43,12 +43,6 @@ Gem::Specification.new do |s|
43
43
  s.require_paths = ["lib"]
44
44
  s.rubygems_version = %q{1.6.2}
45
45
  s.summary = %q{Property sets for ActiveRecord}
46
- s.test_files = [
47
- "test/helper.rb",
48
- "test/schema.rb",
49
- "test/test_property_sets.rb",
50
- "test/test_view_extensions.rb"
51
- ]
52
46
 
53
47
  if s.respond_to? :specification_version then
54
48
  s.specification_version = 3
data/test/schema.rb CHANGED
@@ -29,6 +29,16 @@ ActiveRecord::Schema.define(:version => 1) do
29
29
 
30
30
  add_index :account_validations, [ :account_id, :name ], :unique => true
31
31
 
32
+ create_table "account_typed_data", :force => true do |t|
33
+ t.integer "account_id"
34
+ t.string "name"
35
+ t.string "value"
36
+ t.datetime "created_at"
37
+ t.datetime "updated_at"
38
+ end
39
+
40
+ add_index :account_typed_data, [ :account_id, :name ], :unique => true
41
+
32
42
  create_table "accounts", :force => true do |t|
33
43
  t.string "name"
34
44
  t.datetime "created_at"
@@ -15,7 +15,7 @@ class TestPropertySets < ActiveSupport::TestCase
15
15
  end
16
16
 
17
17
  should "register the property sets used on a class" do
18
- assert_equal [ :settings, :texts, :validations ], Account.property_set_index
18
+ assert_equal [ :settings, :texts, :validations, :typed_data ], Account.property_set_index
19
19
  end
20
20
 
21
21
  should "support protecting attributes" do
@@ -102,7 +102,6 @@ class TestPropertySets < ActiveSupport::TestCase
102
102
 
103
103
  should "coerce everything but nil to string" do
104
104
  @account.settings.foo = 3
105
- assert @account.settings.foo == 3
106
105
  @account.save
107
106
  assert @account.settings.foo == "3"
108
107
  @account.settings.foo = nil
@@ -208,5 +207,52 @@ class TestPropertySets < ActiveSupport::TestCase
208
207
  assert !@account.settings.pro?
209
208
  end
210
209
  end
210
+
211
+ context "typed columns" do
212
+ context "string data" do
213
+ should "be writable and readable" do
214
+ @account.typed_data.string_prop = "foo"
215
+ assert_equal "foo", @account.typed_data.string_prop
216
+ end
217
+ end
218
+
219
+ context "floating point data" do
220
+ should "be writable and readable" do
221
+ @account.typed_data.float_prop = 1.97898
222
+ assert_equal 1.97898, @account.typed_data.float_prop
223
+ @account.save!
224
+ assert_equal 1.97898, @account.typed_data.float_prop
225
+ end
226
+ end
227
+
228
+ context "integer data" do
229
+ should "be writable and readable" do
230
+ @account.typed_data.int_prop = 25
231
+ assert_equal 25, @account.typed_data.int_prop
232
+ @account.save!
233
+ assert_equal 25, @account.typed_data.int_prop
234
+
235
+ assert_equal "25", @account.typed_data.lookup("int_prop").value
236
+ end
237
+ end
238
+
239
+ context "datetime data" do
240
+ should "be writable and readable" do
241
+ ts = Time.at(Time.now.to_i)
242
+ @account.typed_data.datetime_prop = ts
243
+ assert_equal ts, @account.typed_data.datetime_prop
244
+ @account.save!
245
+ assert_equal ts, @account.typed_data.datetime_prop
246
+ end
247
+
248
+ should "store data in UTC" do
249
+ ts = Time.at(Time.now.to_i)
250
+ string_rep = ts.in_time_zone("UTC").to_s
251
+ @account.typed_data.datetime_prop = ts
252
+ @account.save!
253
+ assert_equal string_rep, @account.typed_data.lookup("datetime_prop").value
254
+ end
255
+ end
256
+ end
211
257
  end
212
258
  end
@@ -71,6 +71,25 @@ class TestViewExtensions < ActiveSupport::TestCase
71
71
  end
72
72
  end
73
73
 
74
+ context "text_field" do
75
+ should "call with :value set" do
76
+ settings = stub(@property => "hello")
77
+ object = stub()
78
+ object.stubs(@association).returns(settings)
79
+
80
+ options = {
81
+ :value => "im text", :name => "object_name[#{@association}][#{@property}]",
82
+ :id => "object_name_#{@association}_#{@property}", :object => object
83
+ }
84
+ template = stub()
85
+ template.expects(:instance_variable_get).with("@object_name").returns(object)
86
+ template.expects(:text_field).with("object_name", @property, options)
87
+
88
+ @proxy.stubs(:template).returns(template)
89
+ @proxy.text_field(@property, options)
90
+ end
91
+ end
92
+
74
93
  context "radio_button" do
75
94
  should "call with checked true for a truth value" do
76
95
  settings = stub(@property => "hello")
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: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 1
10
- version: 0.5.1
9
+ - 2
10
+ version: 0.5.2
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-04-26 00:00:00 -07:00
18
+ date: 2011-08-03 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -205,8 +205,5 @@ rubygems_version: 1.6.2
205
205
  signing_key:
206
206
  specification_version: 3
207
207
  summary: Property sets for ActiveRecord
208
- test_files:
209
- - test/helper.rb
210
- - test/schema.rb
211
- - test/test_property_sets.rb
212
- - test/test_view_extensions.rb
208
+ test_files: []
209
+