custom-attributes 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -2,9 +2,13 @@ source :gemcutter
2
2
 
3
3
  # Rails 3.0
4
4
  gem 'rails', '3.0.0'
5
- gem 'rspec', '2.0.0.beta.22'
5
+ gem 'rspec', '2.0.0'
6
6
  gem 'sqlite3-ruby', :require => 'sqlite3'
7
7
  gem 'mysql'
8
8
  gem 'pg'
9
9
  gem 'jeweler'
10
- gem 'rcov'
10
+ gem 'rcov'
11
+
12
+ group :test do
13
+ gem 'formtastic'
14
+ end
@@ -34,6 +34,10 @@ GEM
34
34
  diff-lcs (1.1.2)
35
35
  erubis (2.6.6)
36
36
  abstract (>= 1.0.0)
37
+ formtastic (1.1.0)
38
+ actionpack (>= 2.3.0)
39
+ activesupport (>= 2.3.0)
40
+ i18n (>= 0.4.0)
37
41
  gemcutter (0.6.1)
38
42
  git (1.2.5)
39
43
  i18n (0.4.1)
@@ -42,7 +46,7 @@ GEM
42
46
  git (>= 1.2.5)
43
47
  rubyforge (>= 2.0.0)
44
48
  json_pure (1.4.6)
45
- mail (2.2.6.1)
49
+ mail (2.2.7)
46
50
  activesupport (>= 2.3.6)
47
51
  mime-types
48
52
  treetop (>= 1.4.5)
@@ -70,20 +74,20 @@ GEM
70
74
  thor (~> 0.14.0)
71
75
  rake (0.8.7)
72
76
  rcov (0.9.9)
73
- rspec (2.0.0.beta.22)
74
- rspec-core (= 2.0.0.beta.22)
75
- rspec-expectations (= 2.0.0.beta.22)
76
- rspec-mocks (= 2.0.0.beta.22)
77
- rspec-core (2.0.0.beta.22)
78
- rspec-expectations (2.0.0.beta.22)
77
+ rspec (2.0.0)
78
+ rspec-core (= 2.0.0)
79
+ rspec-expectations (= 2.0.0)
80
+ rspec-mocks (= 2.0.0)
81
+ rspec-core (2.0.0)
82
+ rspec-expectations (2.0.0)
79
83
  diff-lcs (>= 1.1.2)
80
- rspec-mocks (2.0.0.beta.22)
81
- rspec-core (= 2.0.0.beta.22)
82
- rspec-expectations (= 2.0.0.beta.22)
84
+ rspec-mocks (2.0.0)
85
+ rspec-core (= 2.0.0)
86
+ rspec-expectations (= 2.0.0)
83
87
  rubyforge (2.0.4)
84
88
  json_pure (>= 1.1.7)
85
89
  sqlite3-ruby (1.3.1)
86
- thor (0.14.2)
90
+ thor (0.14.3)
87
91
  treetop (1.4.8)
88
92
  polyglot (>= 0.3.1)
89
93
  tzinfo (0.3.23)
@@ -92,10 +96,11 @@ PLATFORMS
92
96
  ruby
93
97
 
94
98
  DEPENDENCIES
99
+ formtastic
95
100
  jeweler
96
101
  mysql
97
102
  pg
98
103
  rails (= 3.0.0)
99
104
  rcov
100
- rspec (= 2.0.0.beta.22)
105
+ rspec (= 2.0.0)
101
106
  sqlite3-ruby
@@ -12,7 +12,7 @@ Contents of the package
12
12
 
13
13
  * Extension for ActiveRecord
14
14
  * Extension for Formtastic (including jQuery widget)
15
- * Extension for Cucumber
15
+ * Extension for Cucumber for integration testing
16
16
 
17
17
  Usage
18
18
  =====
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -108,10 +108,15 @@ module ActiveRecord
108
108
  end
109
109
 
110
110
  module InstanceMethods
111
+
111
112
  def custom_attributes
112
113
  @custom_attributes ||= ActiveRecord::CustomAttributes::CustomAttributeList.new(self)
113
114
  end
114
115
 
116
+ def custom_attributes= post_data
117
+ custom_attributes.set_post_data post_data
118
+ end
119
+
115
120
  private
116
121
 
117
122
  def cache_custom_attributes
@@ -4,16 +4,35 @@ 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
8
  load if @attribute_model
8
9
  end
9
10
 
10
- attr_accessor :type, :value, :label, :internal_label
11
+ [:type, :value, :label, :internal_label].each do |property|
12
+ attr_accessor property
13
+
14
+ define_method "#{property}_with_activation=" do |new_value|
15
+ @marked_for_deletion = false
16
+ send("#{property}_without_activation=".to_sym, new_value)
17
+ end
18
+ alias_method_chain "#{property}=".to_sym, :activation
19
+ end
11
20
 
12
21
  def rename_to new_name
13
22
  item_list.rename_label_of self, new_name
14
23
  end
15
24
 
25
+ def mark_for_deletion
26
+ @marked_for_deletion = true
27
+ end
28
+
29
+ def marked_for_deletion?
30
+ @marked_for_deletion
31
+ end
32
+
16
33
  def save
34
+ attribute_model.destroy and return if marked_for_deletion?
35
+
17
36
  attribute_model.value_type = type.to_s
18
37
  attribute_model.field_name = internal_label.to_s
19
38
  attribute_model.field_label = label
@@ -7,8 +7,25 @@ class ActiveRecord::CustomAttributes::CustomAttributeList
7
7
  define_attribute_methods
8
8
  end
9
9
 
10
+ def set_post_data(post_data)
11
+ loaded_attributes.each(&:mark_for_deletion)
12
+ post_data.each do |attribute_type, values|
13
+ attribute_type = attribute_type.to_sym
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]
20
+ end
21
+
22
+ end # TODO: Now what? Custom defined attributes also come here. Or should that be changed?
23
+ end
24
+ end
25
+
10
26
  def add(type, label, value)
11
27
  type = type.to_sym
28
+ return if label.blank? and value.respond_to? :blank? and value.blank?
12
29
  internal_label = convert_to_internal_label(type, label)
13
30
  attribute = get_attribute(type, internal_label || label, true)
14
31
  attribute.value = value
@@ -58,7 +75,7 @@ class ActiveRecord::CustomAttributes::CustomAttributeList
58
75
  end
59
76
 
60
77
  def attributes_of_type(type)
61
- loaded_attributes.select { |i| i.type == type.to_sym }
78
+ loaded_attributes.select { |i| i.type == type.to_sym and !i.marked_for_deletion? }
62
79
  end
63
80
 
64
81
  private
@@ -104,6 +121,7 @@ class ActiveRecord::CustomAttributes::CustomAttributeList
104
121
 
105
122
  def get_value_of(type, internal_label)
106
123
  found = get_attribute(type, internal_label)
124
+ return nil if found and found.marked_for_deletion?
107
125
  found.value if found
108
126
  end
109
127
 
@@ -123,7 +123,11 @@ module Formtastic
123
123
  end
124
124
 
125
125
  def field_name_for(attribute_type, field_type)
126
- "#{@object.class.model_name.underscore}[custom_attributes][#{attribute_type}][#{field_type}][]"
126
+ if @object.custom_attributes.supported_attribute_types.keys.include? attribute_type
127
+ "#{@object.class.model_name.underscore}[custom_attributes][#{attribute_type}][#{field_type}][]"
128
+ else
129
+ "#{@object.class.model_name.underscore}[#{attribute_type}][#{field_type}][]"
130
+ end
127
131
  end
128
132
 
129
133
  def custom_field_input(attribute_type, field_method_priority, label, value, options = {})
@@ -21,7 +21,7 @@ describe "Custom attributes of a person" do
21
21
  clean_database!
22
22
 
23
23
  I18n.backend.store_translations :'nl', {
24
- :activerecord => { :custom_attributes => { :person => { :telephone => { :private => "Prive" }}}}
24
+ :activerecord => {:custom_attributes => {:person => {:telephone => {:private => "Prive"}}}}
25
25
  }
26
26
  I18n.default_locale = 'nl'
27
27
 
@@ -43,7 +43,7 @@ describe "Custom attributes of a person" do
43
43
  it "should cache the 'born on' date locally" do
44
44
  @person.custom_attributes.add_date "Born on", Date.civil(1981, 5, 31)
45
45
  @person.save
46
- @person.born_on.should == Date.civil(1981,5, 31)
46
+ @person.born_on.should == Date.civil(1981, 5, 31)
47
47
  end
48
48
 
49
49
  it "should store values in the database" do
@@ -83,6 +83,44 @@ describe "Custom attributes of a person" do
83
83
  @person.custom_attributes.defined_labels_for(:date).should =~ ["Born on", "Wed on", "Died on"]
84
84
  end
85
85
 
86
+ it "should mark attributes for deletion" do
87
+ @person.custom_attributes.add_telephone "Prive", "06 28 61 06 28"
88
+
89
+ fields = @person.custom_attributes.telephone_attributes
90
+ fields.should have(1).item
91
+ fields[0].mark_for_deletion
92
+
93
+ deleted_fields = @person.custom_attributes.telephone_attributes
94
+ deleted_fields.should have(0).items
95
+
96
+ @person.custom_attributes.telephone_value_of(:private).should == nil
97
+ end
98
+
99
+
100
+ it "should accept post data in an custom_attributes hash" do
101
+ # field format is #{@object.class.model_name.underscore}[custom_attributes][#{attribute_type}][#{field_type}][]
102
+
103
+ custom_attribute_post_data = {
104
+ "telephone" => {
105
+ "label" => ["Prive", "Werk", ""],
106
+ "value" => ["06 28 61 06 28", "1234567890", ""]
107
+ },
108
+ "email" => {
109
+ "label" => ["Prive", ""],
110
+ "value" => ["matthijs.groen@gmail.com", ""]
111
+ }
112
+ }
113
+
114
+ @person.custom_attributes = custom_attribute_post_data
115
+
116
+ fields = @person.custom_attributes.telephone_attributes
117
+ fields.should have(2).items
118
+ fields[0].value.should == "06 28 61 06 28"
119
+ fields[1].value.should == "1234567890"
120
+
121
+
122
+ end
123
+
86
124
  end
87
125
 
88
126
  describe "Custom attributes of a product" do
@@ -17,7 +17,6 @@ class Product < ActiveRecord::Base
17
17
  fields.url :details, :on_model => { :details => :details_url }
18
18
  end
19
19
 
20
-
21
20
  end
22
21
 
23
22
  class Location < ActiveRecord::Base
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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthijs Groen