custom-attributes 0.2.18 → 0.2.22

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.18
1
+ 0.2.22
@@ -1,11 +1,11 @@
1
1
  class ActiveRecord::CustomAttributes::CustomAttribute
2
2
 
3
3
  def initialize(item_list, main_model, attribute_model)
4
- @main_model = main_model
5
- @item_list = item_list
6
- @attribute_model = attribute_model
4
+ @main_model = main_model
5
+ @item_list = item_list
6
+ @attribute_model = attribute_model
7
7
  @marked_for_destruction = false
8
- @errors = ActiveModel::Errors.new(self)
8
+ @errors = ActiveModel::Errors.new(self)
9
9
  load if @attribute_model
10
10
  end
11
11
 
@@ -64,24 +64,24 @@ class ActiveRecord::CustomAttributes::CustomAttribute
64
64
  attr_reader :main_model, :item_list
65
65
 
66
66
  FIELD_MAPPING = {
67
- :text => :text,
68
- :string => :text,
69
- :float => :float,
70
- :number => :number,
71
- :boolean => :number,
67
+ :text => :text,
68
+ :string => :text,
69
+ :float => :float,
70
+ :number => :number,
71
+ :boolean => :number,
72
72
  :date_time => :date_time,
73
- :date => :date_time,
74
- :time => :date_time
73
+ :date => :date_time,
74
+ :time => :date_time
75
75
  }
76
76
 
77
77
  def assign_model_value
78
- attribute_model.value_type = type.to_s
79
- attribute_model.field_name = internal_label.to_s
78
+ attribute_model.value_type = type.to_s
79
+ attribute_model.field_name = internal_label.to_s
80
80
  attribute_model.field_label = label
81
- write_value = item_list.supported_attribute_types[type]
82
- field = FIELD_MAPPING[write_value]
81
+ write_value = item_list.supported_attribute_types[type]
82
+ field = FIELD_MAPPING[write_value]
83
83
 
84
- converted_value = value
84
+ converted_value = value
85
85
  converted_value = value ? 1 : 0 if write_value == :boolean
86
86
 
87
87
  ([:text, :date_time, :number, :float] - [field]).each { |value_field| attribute_model.send("#{value_field}_value=", nil) }
@@ -89,13 +89,17 @@ class ActiveRecord::CustomAttributes::CustomAttribute
89
89
  end
90
90
 
91
91
  def load
92
- self.type = attribute_model.value_type.to_sym
93
- self.internal_label = attribute_model.field_name.to_sym
92
+ self.type = attribute_model.value_type.to_sym
93
+ if attribute_model.field_name.nil? or attribute_model.field_name.blank?
94
+ self.internal_label = nil
95
+ else
96
+ self.internal_label = attribute_model.field_name.to_sym
97
+ end
94
98
  self.label = attribute_model.field_label
95
99
 
96
100
  read_value = item_list.supported_attribute_types[self.type]
97
- field = FIELD_MAPPING[read_value]
98
- value = attribute_model.send("#{field}_value")
101
+ field = FIELD_MAPPING[read_value]
102
+ value = attribute_model.send("#{field}_value")
99
103
  value = value == 0 ? false : true if read_value == :boolean
100
104
  #puts "#{self.type} => #{read_value} = (#{field}) #{value} - #{self.label} (#{self.internal_label})"
101
105
  self.value = value
@@ -59,7 +59,7 @@ class ActiveRecord::CustomAttributes::CustomAttributeList
59
59
  end
60
60
 
61
61
  def get_custom_validations_for(type, name)
62
- [defined_validations[type], defined_attributes[type][name][:validate_with]].compact
62
+ [defined_validations[type], (defined_attributes[type][name] || {})[:validate_with]].compact
63
63
  end
64
64
 
65
65
  def defined_attribute_types
@@ -38,7 +38,7 @@ module Formtastic
38
38
 
39
39
  index = 0
40
40
  value_fields = @object.custom_attributes.attributes_of_type(attribute_type).collect do |attribute|
41
- result = custom_field_input(attribute_type, [attribute_type, storage_type], attribute.label, attribute.value, index, {})
41
+ result = custom_field_input(attribute_type, [attribute_type, storage_type], attribute.label, attribute.value, index, { :errors => attribute.errors })
42
42
  index += 1
43
43
  result
44
44
  end
@@ -163,22 +163,47 @@ module Formtastic
163
163
  ## Field addition support
164
164
 
165
165
  def custom_string_input(attribute_type, value, index, options = {})
166
- default_custom_field_handler(:text_field_tag, attribute_type, value, index)
166
+ default_custom_field_handler(:text_field_tag, attribute_type, value, index, options)
167
167
  end
168
168
 
169
169
  def custom_email_input(attribute_type, value, index, options = {})
170
- default_custom_field_handler(:email_field_tag, attribute_type, value, index)
170
+ default_custom_field_handler(:email_field_tag, attribute_type, value, index, options)
171
171
  end
172
172
 
173
173
  def custom_url_input(attribute_type, value, index, options = {})
174
- default_custom_field_handler(:url_field_tag, attribute_type, value, index)
174
+ default_custom_field_handler(:url_field_tag, attribute_type, value, index, options)
175
175
  end
176
176
 
177
- def default_custom_field_handler(method, attribute_type, value, index)
177
+ def custom_date_input(attribute_type, value, index, options = {})
178
178
  i18n_scope = [:activerecord, :custom_attributes, @object.class.model_name.underscore.to_sym]
179
179
  attribute_human_name = ::I18n.t(attribute_type, :count => 1, :scope => i18n_scope + [:attribute_names]).capitalize
180
+ format = options[:format] || I18n.t(:default, :scope => [:date, :formats]) || '%d %b %Y'
181
+
182
+ error_listing = ""
183
+ if options[:errors]
184
+ errors = [options[:errors][:value]]
185
+ errors = errors.flatten.compact.uniq
186
+ error_listing = send(:"error_#{self.class.inline_errors}", [*errors]) if errors.any?
187
+ end
188
+
189
+ str_value = value.respond_to?(:strftime) ? value.try(:strftime, format) : value
190
+
191
+ template.text_field_tag(field_name_for(attribute_type, "value", index), str_value, :class => 'custom-datepicker') <<
192
+ custom_attribute_delete_link(attribute_human_name) << error_listing
193
+ end
194
+
195
+ def default_custom_field_handler(method, attribute_type, value, index, options)
196
+ i18n_scope = [:activerecord, :custom_attributes, @object.class.model_name.underscore.to_sym]
197
+ attribute_human_name = ::I18n.t(attribute_type, :count => 1, :scope => i18n_scope + [:attribute_names]).capitalize
198
+ error_listing = ""
199
+ if options[:errors]
200
+ errors = [options[:errors][:value]]
201
+ errors = errors.flatten.compact.uniq
202
+ error_listing = send(:"error_#{self.class.inline_errors}", [*errors]) if errors.any?
203
+ end
204
+
180
205
  template.send(method, field_name_for(attribute_type, "value", index), value) <<
181
- custom_attribute_delete_link(attribute_human_name)
206
+ custom_attribute_delete_link(attribute_human_name) << error_listing
182
207
  end
183
208
 
184
209
  delegate :content_tag, :to => :template
@@ -59,6 +59,14 @@ describe "Custom attributes of a person" do
59
59
  loaded_person.custom_attributes.telephone_value_of(:private).should == "06 28 61 06 28"
60
60
  end
61
61
 
62
+ it "should accept custom defined labels" do
63
+ @person.custom_attributes.add_telephone "Werk", "06 28 61 06 28"
64
+ @person.save
65
+
66
+ loaded_person = Person.find @person.id
67
+ loaded_person.custom_attributes.telephone_value_of("Werk").should == "06 28 61 06 28"
68
+ end
69
+
62
70
  it "should rename labels" do
63
71
  @person.custom_attributes.add_telephone "Werk", "06 28 61 06 28"
64
72
 
@@ -241,4 +249,9 @@ describe "Custom attributes of a product" do
241
249
  @product.should be_valid
242
250
  end
243
251
 
252
+ it "should not run custom validations if name differs" do
253
+ @product.custom_attributes.add_date "In stock sance", Date.civil(2009, 12, 31)
254
+ @product.should be_valid
255
+ end
256
+
244
257
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: custom-attributes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 59
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 18
10
- version: 0.2.18
9
+ - 22
10
+ version: 0.2.22
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthijs Groen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-27 00:00:00 +02:00
18
+ date: 2010-10-28 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21