@@ -7,7 +7,9 @@ class Child < CouchRestRails::Document
7
7
Sunspot ::Adapters ::InstanceAdapter . register ( DocumentInstanceAccessor , Child )
8
8
9
9
before_save :initialize_history , :if => :new?
10
+ before_save :update_photo_keys
10
11
before_save :update_history , :unless => :new?
12
+
11
13
property :age
12
14
property :name
13
15
property :nickname
@@ -22,15 +24,15 @@ class Child < CouchRestRails::Document
22
24
}
23
25
}"
24
26
25
- validates_with_method :validate_file_name
27
+ validates_with_method :validate_photos
26
28
validates_with_method :validate_audio_file_name
27
29
validates_fields_of_type Field ::NUMERIC_FIELD
28
30
validates_fields_of_type Field ::TEXT_FIELD
29
31
validates_fields_of_type Field ::TEXT_AREA
30
32
validates_fields_of_type Field ::DATE_FIELD
31
33
validates_with_method :validate_has_at_least_one_field_value
32
34
validates_with_method :created_at , :method => :validate_created_at
33
-
35
+
34
36
def self . build_solar_schema
35
37
fields = build_fields_for_solar
36
38
Sunspot . setup ( Child ) do
@@ -49,8 +51,13 @@ def validate_has_at_least_one_field_value
49
51
[ false , "Please fill in at least one field or upload a file" ]
50
52
end
51
53
52
- def validate_file_name
53
- return true if @file_name == nil || /([^\s ]+(\. (?i)(jpg|jpeg|png))$)/ =~ @file_name
54
+ def validate_age
55
+ return true if age . nil? || age . blank? || !age . is_number? || ( age =~ /^\d {1,2}(\. \d )?$/ && age . to_f > 0 && age . to_f < 100 )
56
+ [ false , "Age must be between 1 and 99" ]
57
+ end
58
+
59
+ def validate_photos
60
+ return true if @photos . blank? || @photos . all? { |photo | /image\/ (jpg|jpeg|png)/ =~ photo . content_type }
54
61
[ false , "Please upload a valid photo file (jpg or png) for this child record" ]
55
62
end
56
63
@@ -68,9 +75,9 @@ def validate_created_at
68
75
rescue
69
76
[ false , '' ]
70
77
end
71
- end
72
-
73
- def method_missing ( m , *args , &block )
78
+ end
79
+
80
+ def method_missing ( m , *args , &block )
74
81
self [ m ]
75
82
end
76
83
@@ -125,30 +132,54 @@ def unique_identifier
125
132
end
126
133
127
134
def rotate_photo ( angle )
128
- exisiting_photo = photo
129
- image = MiniMagick ::Image . from_blob ( exisiting_photo . data . read )
135
+ existing_photo = primary_photo
136
+ image = MiniMagick ::Image . from_blob ( existing_photo . data . read )
130
137
image . rotate ( angle )
131
-
132
- name = FileAttachment . generate_name
133
- attachment = FileAttachment . new ( name , exisiting_photo . content_type , image . to_blob )
134
- attach ( attachment , 'current_photo_key' )
138
+
139
+ attachment = FileAttachment . new ( existing_photo . name , existing_photo . content_type , image . to_blob )
140
+ # attachment = FileAttachment.from_uploadable_file(image.to_blob, "photo-#{existing_photo.name.hash}")
141
+
142
+ self [ 'photo_keys' ] . delete ( attachment . name )
143
+ @photo_keys = [ attachment . name ]
144
+ delete_attachment ( existing_photo . name )
145
+ attach ( attachment )
146
+ end
147
+
148
+ def delete_photo ( delete_photos )
149
+ return unless delete_photos
150
+ delete_photos . keys . collect do |delete_photo |
151
+ self [ 'photo_keys' ] . delete ( delete_photo )
152
+ end
135
153
end
154
+
155
+ def photo = ( new_photos )
156
+ return unless new_photos
157
+ #basically to support any client passing a single photo param, only used by child_spec AFAIK
158
+ unless new_photos . is_a? Hash
159
+ new_photos = { '0' => new_photos }
160
+ end
136
161
137
- def photo = ( photo_file )
138
- return unless photo_file . respond_to? :content_type
139
- @file_name = photo_file . original_path
140
- attachment = FileAttachment . from_uploadable_file ( photo_file , "photo" )
141
- attach ( attachment , 'current_photo_key' )
162
+ @photos = [ ]
163
+ @photo_keys = new_photos . values . select { |photo | photo . respond_to? :content_type } . collect do |photo |
164
+ @photos << photo
165
+ attachment = FileAttachment . from_uploadable_file ( photo , "photo-#{ photo . path . hash } " )
166
+ attach ( attachment )
167
+ attachment . name
168
+ end
142
169
end
143
170
144
- def photo
145
- attachment_name = self [ 'current_photo_key' ]
146
- return if attachment_name . blank?
147
- data = read_attachment attachment_name
148
- content_type = self [ '_attachments' ] [ attachment_name ] [ 'content_type' ]
149
- FileAttachment . new attachment_name , content_type , data
171
+ def photos
172
+ return [ ] if self [ 'photo_keys' ] . blank?
173
+ self [ 'photo_keys' ] . collect do |key |
174
+ attachment ( key )
175
+ end
150
176
end
151
-
177
+
178
+ def primary_photo
179
+ key = self [ 'current_photo_key' ]
180
+ key ? attachment ( key ) : nil
181
+ end
182
+
152
183
def audio
153
184
return nil if self [ 'audio_attachments' ] . nil?
154
185
attachment_key = self [ 'audio_attachments' ] [ 'original' ]
@@ -163,15 +194,15 @@ def audio=(audio_file)
163
194
return unless audio_file . respond_to? :content_type
164
195
@audio_file_name = audio_file . original_path
165
196
attachment = FileAttachment . from_uploadable_file ( audio_file , "audio" )
166
-
167
- attach ( attachment , attachment . name )
197
+ self [ 'recorded_audio' ] = attachment . name
198
+ attach ( attachment )
168
199
setup_original_audio ( attachment )
169
200
setup_mime_specific_audio ( attachment )
170
201
end
171
202
172
203
def add_audio_file ( audio_file , content_type )
173
204
attachment = FileAttachment . from_file ( audio_file , content_type , "audio" , key_for_content_type ( content_type ) )
174
- attach ( attachment , attachment . name )
205
+ attach ( attachment )
175
206
setup_mime_specific_audio ( attachment )
176
207
end
177
208
@@ -181,11 +212,12 @@ def media_for_key(media_key)
181
212
FileAttachment . new media_key , content_type , data
182
213
end
183
214
184
- def update_properties_with_user_name ( user_name , new_photo , new_audio , properties )
215
+ def update_properties_with_user_name ( user_name , new_photo , delete_photo , new_audio , properties )
185
216
properties . each_pair do |name , value |
186
217
self [ name ] = value unless value == nil
187
218
end
188
219
self . set_updated_fields_for user_name
220
+ self . delete_photo ( delete_photo )
189
221
self . photo = new_photo
190
222
self . audio = new_audio
191
223
end
@@ -235,21 +267,36 @@ def field_name_changes
235
267
def changed? ( field_name )
236
268
return false if self [ field_name ] . blank? && @from_child [ field_name ] . blank?
237
269
return true if @from_child [ field_name ] . blank?
238
- self [ field_name ] . strip != @from_child [ field_name ] . strip
270
+ if self [ field_name ] . respond_to? :strip
271
+ self [ field_name ] . strip != @from_child [ field_name ] . strip
272
+ else
273
+ self [ field_name ] != @from_child [ field_name ]
274
+ end
239
275
end
240
276
241
277
def is_filled_in? field
242
278
!( self [ field . name ] . nil? || self [ field . name ] == field . default_value )
243
279
end
244
280
245
281
private
246
- def attach ( attachment , key )
247
- self [ key ] = attachment . name
282
+ def attachment ( key )
283
+ data = read_attachment key
284
+ content_type = self [ '_attachments' ] [ key ] [ 'content_type' ]
285
+ FileAttachment . new key , content_type , data
286
+ end
287
+
288
+ def update_photo_keys
289
+ @photo_keys ||= [ ]
290
+ self [ 'photo_keys' ] ||= [ ]
291
+ self [ 'photo_keys' ] . concat @photo_keys
292
+ self [ 'current_photo_key' ] = @photo_keys . first || self [ 'photo_keys' ] . first
293
+ end
294
+
295
+ def attach ( attachment )
248
296
create_attachment :name => attachment . name ,
249
297
:content_type => attachment . content_type ,
250
- :file => attachment . data
251
-
252
- end
298
+ :file => attachment . data
299
+ end
253
300
254
301
def deprecated_fields
255
302
system_fields = [ "created_at" , "posted_at" , "posted_from" , "_rev" , "_id" , "created_by" , "couchrest-type" , "histories" , "unique_identifier" ]
0 commit comments