person-name 0.2.7 → 0.2.8

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.7
1
+ 0.2.8
@@ -37,9 +37,10 @@ module PersonName
37
37
  module Core
38
38
 
39
39
  def self.included(base)
40
- base.send :include, PersonName::ActiveRecord::Core::InstanceMethods
41
40
  base.extend PersonName::ActiveRecord::Core::ClassMethods
41
+ base.send :include, PersonName::ActiveRecord::Core::InstanceMethods
42
42
  base.initialize_person_names
43
+
43
44
  base.before_validation :start_name_validation
44
45
  base.after_validation :stop_name_validation
45
46
  #base.define_scopes
@@ -86,6 +87,54 @@ module PersonName
86
87
 
87
88
  module InstanceMethods
88
89
 
90
+ # when using forms to edit the full name or the indiviual form parts
91
+ # it is possible that both fields (The aggregated one _and_ the individual ones)
92
+ # get posted back to the model. So, wich fields are the correct edited ones?
93
+ #
94
+ # To determine this, we use this development mantra:
95
+ # 1. If the user is able to edit it in one field, he should.
96
+ # Therefore, if the aggregated field is posted, this is the most recent one
97
+ # and should override the seperated parts
98
+ # 2. If only individual parts are edited, there should not be an aggregated
99
+ # value in the form
100
+ # 3. If the view allowes the user to edit the aggregated field and the seperated fields,
101
+ # the field should make sure the values are comparable (eg. the separated values combined
102
+ # should be equal to the aggregated one). If this is the case, the separated fields
103
+ # will rule over the aggregated one, since it is more detailed and adds up to the same result.
104
+ def attributes=(new_attributes, guard_protected_attributes = true)
105
+ return unless new_attributes.is_a?(Hash)
106
+ attributes = new_attributes.stringify_keys
107
+
108
+ self.class.name_types.map(&:to_s).each do |name_base|
109
+ if attributes.has_key? name_base # only add behaviour if the full name is supplied
110
+ full = attributes[name_base]
111
+
112
+ existing_part_fields = []
113
+ part_values = []
114
+ PersonName::NameSplitter::NAME_PARTS.each do |name_part|
115
+ test_field = "#{name_base}_#{name_part}"
116
+ if attributes.has_key? test_field
117
+ existing_part_fields << test_field
118
+ part_values << attributes[test_field]
119
+ else
120
+ part_values << send(test_field.to_sym)
121
+ end
122
+ end
123
+ if part_values.compact.join(" ") == full
124
+ attributes.delete name_base
125
+ else
126
+ existing_part_fields.each do |part_field|
127
+ attributes.delete part_field
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ super attributes, guard_protected_attributes
134
+ end
135
+
136
+ private
137
+
89
138
  def person_name_for field
90
139
  @person_names ||= {}
91
140
  @person_names[field] ||= PersonName::ActiveRecordPersonName.new(field, self)
@@ -97,8 +146,6 @@ module PersonName
97
146
  person_name_for(field).full_name = new_name
98
147
  end
99
148
 
100
- private
101
-
102
149
  def start_name_validation
103
150
  @name_validation = true
104
151
  end
@@ -124,6 +124,31 @@ describe "Has Person Name" do
124
124
  @person.should be_valid
125
125
  end
126
126
 
127
+ it "should use the full name if post attributes don't match" do
128
+ @person = NamePerson.new
129
+ @person.attributes = {
130
+ :name_first_name => "Henk",
131
+ :name_middle_name => "Jacobus",
132
+ :name_last_name => "Groen",
133
+ :name => "Matthijs Jacobus Groen"
134
+ }
135
+ @person.name.to_s.should == "Matthijs Jacobus Groen"
136
+ end
137
+
138
+ it "should use the individual fields if post attributes match" do
139
+ @person = NamePerson.new
140
+ @person.attributes = {
141
+ :name => "Matthijs Jacobus Groen",
142
+ :name_first_name => "Matthijs",
143
+ :name_middle_name => nil,
144
+ :name_last_name => "Jacobus Groen"
145
+ }
146
+ @person.name.to_s.should == "Matthijs Jacobus Groen"
147
+ @person.name.last_name.should == "Jacobus Groen"
148
+
149
+ end
150
+
151
+
127
152
  end
128
153
 
129
154
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: person-name
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 7
10
- version: 0.2.7
9
+ - 8
10
+ version: 0.2.8
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-25 00:00:00 +02:00
18
+ date: 2010-10-26 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21