custom-attributes 0.2.5 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,15 @@ or weight and size measurements to products.
7
7
  Most of these fields can be used purely dynamic and objects could have multiples of them (think telephone numbers, or email
8
8
  addresses)
9
9
 
10
+ This code is *NOT* production ready yet!
11
+
12
+ TODO
13
+ ====
14
+ - Make Formtastic extension better
15
+ - Supply specs for the formtastic part
16
+ - Make custom extensions work as nested forms (accept_attributes_for stuff)
17
+
18
+
10
19
  Contents of the package
11
20
  =======================
12
21
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.12
@@ -4,7 +4,7 @@ class ActiveRecord::CustomAttributes::CustomAttribute
4
4
  @main_model = main_model
5
5
  @item_list = item_list
6
6
  @attribute_model = attribute_model
7
- @marked_for_deletion = false
7
+ @marked_for_destruction = false
8
8
  load if @attribute_model
9
9
  end
10
10
 
@@ -12,7 +12,7 @@ class ActiveRecord::CustomAttributes::CustomAttribute
12
12
  attr_accessor property
13
13
 
14
14
  define_method "#{property}_with_activation=" do |new_value|
15
- @marked_for_deletion = false
15
+ @marked_for_destruction = false
16
16
  send("#{property}_without_activation=".to_sym, new_value)
17
17
  end
18
18
  alias_method_chain "#{property}=".to_sym, :activation
@@ -22,16 +22,16 @@ class ActiveRecord::CustomAttributes::CustomAttribute
22
22
  item_list.rename_label_of self, new_name
23
23
  end
24
24
 
25
- def mark_for_deletion
26
- @marked_for_deletion = true
25
+ def mark_for_destruction
26
+ @marked_for_destruction = true
27
27
  end
28
28
 
29
- def marked_for_deletion?
30
- @marked_for_deletion
29
+ def marked_for_destruction?
30
+ @marked_for_destruction
31
31
  end
32
32
 
33
33
  def save
34
- attribute_model.destroy and return if marked_for_deletion?
34
+ attribute_model.destroy and return if marked_for_destruction?
35
35
 
36
36
  attribute_model.value_type = type.to_s
37
37
  attribute_model.field_name = internal_label.to_s
@@ -8,18 +8,14 @@ class ActiveRecord::CustomAttributes::CustomAttributeList
8
8
  end
9
9
 
10
10
  def set_post_data(post_data)
11
- loaded_attributes.each(&:mark_for_deletion)
11
+ loaded_attributes.each(&:mark_for_destruction)
12
12
  post_data.each do |attribute_type, values|
13
13
  attribute_type = attribute_type.to_sym
14
14
  if supported_attribute_types.keys.include? attribute_type
15
- label_list = values["label"]
16
- value_list = values["value"]
17
-
18
- label_list.each_with_index do |label, index|
19
- add attribute_type, label, value_list[index]
15
+ values.each do |key, attributes|
16
+ add attribute_type, attributes["label"], attributes["value"] unless key == "%nr%"
20
17
  end
21
-
22
- end # TODO: Now what? Custom defined attributes also come here. Or should that be changed?
18
+ end
23
19
  end
24
20
  end
25
21
 
@@ -75,7 +71,7 @@ class ActiveRecord::CustomAttributes::CustomAttributeList
75
71
  end
76
72
 
77
73
  def attributes_of_type(type)
78
- loaded_attributes.select { |i| i.type == type.to_sym and !i.marked_for_deletion? }
74
+ loaded_attributes.select { |i| i.type == type.to_sym and !i.marked_for_destruction? }
79
75
  end
80
76
 
81
77
  private
@@ -121,7 +117,7 @@ class ActiveRecord::CustomAttributes::CustomAttributeList
121
117
 
122
118
  def get_value_of(type, internal_label)
123
119
  found = get_attribute(type, internal_label)
124
- return nil if found and found.marked_for_deletion?
120
+ return nil if found and found.marked_for_destruction?
125
121
  found.value if found
126
122
  end
127
123
 
@@ -36,10 +36,12 @@ module Formtastic
36
36
  storage_type = @object.custom_attributes.supported_attribute_types[attribute_type]
37
37
  i18n_scope = [:activerecord, :custom_attributes, @object.class.model_name.underscore.to_sym]
38
38
 
39
+ index = 0
39
40
  value_fields = @object.custom_attributes.attributes_of_type(attribute_type).collect do |attribute|
40
- custom_field_input(attribute_type, [attribute_type, storage_type], attribute.label, attribute.value)
41
+ custom_field_input(attribute_type, [attribute_type, storage_type], attribute.label, attribute.value, index, {})
42
+ index += 1
41
43
  end
42
- field_template = custom_field_input(attribute_type, [attribute_type, storage_type], "", nil, :template => true)
44
+ field_template = custom_field_input(attribute_type, [attribute_type, storage_type], "", nil, "%nr%", :template => true)
43
45
 
44
46
  attribute_human_name = ::I18n.t(attribute_type,
45
47
  :count => 1,
@@ -51,7 +53,8 @@ module Formtastic
51
53
  :class => "add-link",
52
54
  :title => ::I18n.t(:add, :scope => :default_actions, :model => attribute_human_name),
53
55
  :'data-attribute-type' => attribute_type
54
- ) << field_template << label_data_list_for(attribute_type, @object.custom_attributes.defined_labels_for(attribute_type)),
56
+ ) << inline_hints_for(attribute_type, {}) <<
57
+ field_template << label_data_list_for(attribute_type, @object.custom_attributes.defined_labels_for(attribute_type)),
55
58
  :class => "field-addition"
56
59
  )
57
60
 
@@ -69,10 +72,13 @@ module Formtastic
69
72
  association = @object.class.reflect_on_association(association_name)
70
73
  storage_type = association.klass.model_name.underscore.to_sym
71
74
 
75
+ index = 0
72
76
  value_fields = @object.send(association_name).collect do |item|
73
- custom_field_input(storage_type, [storage_type], item.name, item)
77
+ result = custom_field_input(storage_type, [storage_type], item.name, item, index, {})
78
+ index += 1
79
+ result
74
80
  end
75
- field_template = custom_field_input(storage_type, [storage_type], "", nil, :template => true)
81
+ field_template = custom_field_input(association_name, [storage_type], "", nil, "%nr%", :template => true)
76
82
 
77
83
  attribute_human_name = association.klass.model_name.human
78
84
  value_fields << content_tag(:li,
@@ -122,24 +128,28 @@ module Formtastic
122
128
 
123
129
  end
124
130
 
125
- def field_name_for(attribute_type, field_type)
131
+ def field_name_for(attribute_type, field_type, index)
126
132
  if @object.custom_attributes.supported_attribute_types.keys.include? attribute_type
127
- "#{@object.class.model_name.underscore}[custom_attributes][#{attribute_type}][#{field_type}][]"
133
+ "#{@object.class.model_name.underscore}[custom_attributes][#{attribute_type}][#{index}][#{field_type}]"
128
134
  else
129
- "#{@object.class.model_name.underscore}[#{attribute_type}][#{field_type}][]"
135
+ "#{@object.class.model_name.underscore}[#{attribute_type}_attributes][#{index}][#{field_type}]"
130
136
  end
131
137
  end
132
138
 
133
- def custom_field_input(attribute_type, field_method_priority, label, value, options = {})
134
- label_name = field_name_for attribute_type, "label"
139
+ def custom_field_input(attribute_type, field_method_priority, label, value, index, options)
140
+ label_name = field_name_for attribute_type, "label", index
135
141
  label_field = template.text_field_tag(label_name, label, :list => "#{attribute_type}-label-suggestions", :autocomplete => "off")
136
142
 
137
143
  input_field_method = field_method_priority.collect { |m| "custom_#{m}_input".to_sym }.find { |m| self.respond_to? m, true }
138
- input_field = send(input_field_method, attribute_type, value, options)
144
+ input_field = send(input_field_method, attribute_type, value, index, options)
139
145
 
140
- content_tag(options[:template] ? :div : :li,
141
- Formtastic::Util.html_safe(content_tag(:div, label_field, :class => "label") <<
142
- input_field) << inline_hints_for(field_method_priority.first, options),
146
+ field_contents = Formtastic::Util.html_safe(
147
+ content_tag(:div, label_field, :class => "label") << input_field
148
+ )
149
+ field_contents << template.hidden_field_tag(field_name_for(attribute_type, "_destroy", index),
150
+ false, :class => "delete_check") unless options[:template]
151
+
152
+ content_tag(options[:template] ? :div : :li, field_contents,
143
153
  :class => options[:template] ? "template" : "value optional #{attribute_type}"
144
154
  )
145
155
  end
@@ -151,22 +161,22 @@ module Formtastic
151
161
 
152
162
  ## Field addition support
153
163
 
154
- def custom_string_input(attribute_type, value, options = {})
155
- default_custom_field_handler(:text_field_tag, attribute_type, value)
164
+ def custom_string_input(attribute_type, value, index, options = {})
165
+ default_custom_field_handler(:text_field_tag, attribute_type, value, index)
156
166
  end
157
167
 
158
- def custom_email_input(attribute_type, value, options = {})
159
- default_custom_field_handler(:email_field_tag, attribute_type, value)
168
+ def custom_email_input(attribute_type, value, index, options = {})
169
+ default_custom_field_handler(:email_field_tag, attribute_type, value, index)
160
170
  end
161
171
 
162
- def custom_url_input(attribute_type, value, options = {})
163
- default_custom_field_handler(:url_field_tag, attribute_type, value)
172
+ def custom_url_input(attribute_type, value, index, options = {})
173
+ default_custom_field_handler(:url_field_tag, attribute_type, value, index)
164
174
  end
165
175
 
166
- def default_custom_field_handler(method, attribute_type, value)
176
+ def default_custom_field_handler(method, attribute_type, value, index)
167
177
  i18n_scope = [:activerecord, :custom_attributes, @object.class.model_name.underscore.to_sym]
168
178
  attribute_human_name = ::I18n.t(attribute_type, :count => 1, :scope => i18n_scope + [:attribute_names]).capitalize
169
- template.send(method, field_name_for(attribute_type, "value"), value) <<
179
+ template.send(method, field_name_for(attribute_type, "value", index), value) <<
170
180
  custom_attribute_delete_link(attribute_human_name)
171
181
  end
172
182
 
@@ -88,7 +88,7 @@ describe "Custom attributes of a person" do
88
88
 
89
89
  fields = @person.custom_attributes.telephone_attributes
90
90
  fields.should have(1).item
91
- fields[0].mark_for_deletion
91
+ fields[0].mark_for_destruction
92
92
 
93
93
  deleted_fields = @person.custom_attributes.telephone_attributes
94
94
  deleted_fields.should have(0).items
@@ -102,12 +102,20 @@ describe "Custom attributes of a person" do
102
102
 
103
103
  custom_attribute_post_data = {
104
104
  "telephone" => {
105
- "label" => ["Prive", "Werk", ""],
106
- "value" => ["06 28 61 06 28", "1234567890", ""]
105
+ "0" => {
106
+ "label" => "Prive",
107
+ "value" => "06 28 61 06 28"
108
+ },
109
+ "1" => {
110
+ "label" => "Werk",
111
+ "value" => "1234567890"
112
+ }
107
113
  },
108
114
  "email" => {
109
- "label" => ["Prive", ""],
110
- "value" => ["matthijs.groen@gmail.com", ""]
115
+ "0" => {
116
+ "label" => "Prive",
117
+ "value" => "matthijs.groen@gmail.com"
118
+ }
111
119
  }
112
120
  }
113
121
 
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: 29
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 12
10
+ version: 0.2.12
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-12 00:00:00 +02:00
18
+ date: 2010-10-13 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21