Skip to content

Commit 860cc77

Browse files
committed
rapidftr#622 rikesh: merge with master
2 parents ae84169 + 19daaba commit 860cc77

File tree

78 files changed

+878
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+878
-582
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ group :cucumber do
2222
gem 'cucumber-rails', '0.3.2'
2323
gem 'hpricot', '0.8.2'
2424
gem 'launchy', '0.4.0'
25+
2526
end
2627

2728
group :test do

app/controllers/child_media_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def find_audio_attachment
4040

4141
def find_photo_attachment
4242
begin
43-
@attachment = params[:id] ? @child.media_for_key(params[:id]) : @child.photo
43+
@attachment = params[:id] ? @child.media_for_key(params[:id]) : @child.primary_photo
4444
rescue => e
4545
p e.inspect
4646
end

app/controllers/children_controller.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def update
115115

116116
new_photo = params[:child].delete(:photo)
117117
new_audio = params[:child].delete(:audio)
118-
@child.update_properties_with_user_name(current_user_name, new_photo, new_audio, params[:child])
118+
@child.update_properties_with_user_name(current_user_name, new_photo, params[:delete_child_photo], new_audio, params[:child])
119119

120120
respond_to do |format|
121121
if @child.save
@@ -161,12 +161,12 @@ def search
161161
end
162162

163163
def export_data
164-
child_ids = params.map{ |k, v| 'selected' == v ? k : nil }.compact
165-
if child_ids.empty?
164+
selected_records = params["selections"] || {}
165+
if selected_records.empty?
166166
raise ErrorResponse.bad_request('You must select at least one record to be exported')
167167
end
168-
169-
children = child_ids.map{ |child_id| Child.get(child_id) }
168+
169+
children = selected_records.sort.map{ |index, child_id| Child.get(child_id) }
170170

171171
if params[:commit] == "Export to Photo Wall"
172172
export_photos_to_pdf(children, "#{file_basename}.pdf")

app/helpers/histories_helper.rb

+8
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ def history_wording(from, to)
44
return "initially set to #{to}" if from.blank?
55
"changed from #{from} to #{to}"
66
end
7+
8+
def flag_change_message(history)
9+
begin
10+
history['changes']['flag_message']['to']
11+
rescue
12+
''
13+
end
14+
end
715
end

app/models/child.rb

+82-35
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class Child < CouchRestRails::Document
77
Sunspot::Adapters::InstanceAdapter.register(DocumentInstanceAccessor, Child)
88

99
before_save :initialize_history, :if => :new?
10+
before_save :update_photo_keys
1011
before_save :update_history, :unless => :new?
12+
1113
property :age
1214
property :name
1315
property :nickname
@@ -22,15 +24,15 @@ class Child < CouchRestRails::Document
2224
}
2325
}"
2426

25-
validates_with_method :validate_file_name
27+
validates_with_method :validate_photos
2628
validates_with_method :validate_audio_file_name
2729
validates_fields_of_type Field::NUMERIC_FIELD
2830
validates_fields_of_type Field::TEXT_FIELD
2931
validates_fields_of_type Field::TEXT_AREA
3032
validates_fields_of_type Field::DATE_FIELD
3133
validates_with_method :validate_has_at_least_one_field_value
3234
validates_with_method :created_at, :method => :validate_created_at
33-
35+
3436
def self.build_solar_schema
3537
fields = build_fields_for_solar
3638
Sunspot.setup(Child) do
@@ -49,8 +51,13 @@ def validate_has_at_least_one_field_value
4951
[false, "Please fill in at least one field or upload a file"]
5052
end
5153

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}
5461
[false, "Please upload a valid photo file (jpg or png) for this child record"]
5562
end
5663

@@ -68,9 +75,9 @@ def validate_created_at
6875
rescue
6976
[false, '']
7077
end
71-
end
72-
73-
def method_missing(m, *args, &block)
78+
end
79+
80+
def method_missing(m, *args, &block)
7481
self[m]
7582
end
7683

@@ -125,30 +132,54 @@ def unique_identifier
125132
end
126133

127134
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)
130137
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
135153
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
136161

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
142169
end
143170

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
150176
end
151-
177+
178+
def primary_photo
179+
key = self['current_photo_key']
180+
key ? attachment(key) : nil
181+
end
182+
152183
def audio
153184
return nil if self['audio_attachments'].nil?
154185
attachment_key = self['audio_attachments']['original']
@@ -163,15 +194,15 @@ def audio=(audio_file)
163194
return unless audio_file.respond_to? :content_type
164195
@audio_file_name = audio_file.original_path
165196
attachment = FileAttachment.from_uploadable_file(audio_file, "audio")
166-
167-
attach(attachment, attachment.name)
197+
self['recorded_audio'] = attachment.name
198+
attach(attachment)
168199
setup_original_audio(attachment)
169200
setup_mime_specific_audio(attachment)
170201
end
171202

172203
def add_audio_file(audio_file, content_type)
173204
attachment = FileAttachment.from_file(audio_file, content_type, "audio", key_for_content_type(content_type))
174-
attach(attachment, attachment.name)
205+
attach(attachment)
175206
setup_mime_specific_audio(attachment)
176207
end
177208

@@ -181,11 +212,12 @@ def media_for_key(media_key)
181212
FileAttachment.new media_key, content_type, data
182213
end
183214

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)
185216
properties.each_pair do |name, value|
186217
self[name] = value unless value == nil
187218
end
188219
self.set_updated_fields_for user_name
220+
self.delete_photo(delete_photo)
189221
self.photo = new_photo
190222
self.audio = new_audio
191223
end
@@ -235,21 +267,36 @@ def field_name_changes
235267
def changed?(field_name)
236268
return false if self[field_name].blank? && @from_child[field_name].blank?
237269
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
239275
end
240276

241277
def is_filled_in? field
242278
!(self[field.name].nil? || self[field.name] == field.default_value)
243279
end
244280

245281
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)
248296
create_attachment :name => attachment.name,
249297
:content_type => attachment.content_type,
250-
:file => attachment.data
251-
252-
end
298+
:file => attachment.data
299+
end
253300

254301
def deprecated_fields
255302
system_fields = ["created_at","posted_at", "posted_from", "_rev", "_id", "created_by", "couchrest-type", "histories", "unique_identifier"]

app/views/children/_check_box.html.erb

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
<%= check_box_tag check_box.tag_name_attribute, "Yes", @child[check_box.name] == "Yes", :id => check_box.tag_name_attribute %>
44
<%= label_tag check_box.tag_name_attribute, check_box.display_name %>
55
<% if check_box.help_text.present?%>
6-
<br />
7-
<span class='help-text-checkbox'>
8-
<%= check_box.help_text%>
9-
</span>
6+
<img class="tool-tip-icon vtip" title="<%=check_box.help_text%>" src="/images/tool_tip_icon.gif"/>
107
<% end%>
118
</p>

app/views/children/_date_field.html.erb

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
<%= text_field_tag date_field.tag_name_attribute, @child[date_field.name] %>
44
<%= javascript_tag "$(document).ready(function(){ $(\"##{date_field.tag_id}\").datepicker({ dateFormat: 'dd M yy' }); });" %>
55
<% if date_field.help_text.present?%>
6-
<br />
7-
<span class='help-text-container'>
8-
<span class='help-text'>
9-
<%=date_field.help_text%>
10-
</span>
11-
</span>
6+
<img class="tool-tip-icon vtip" title="<%=date_field.help_text%>" src="/images/tool_tip_icon.gif"/>
127
<% end%>
138
</p>

app/views/children/_numeric_field.html.erb

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
<%= text_field_tag numeric_field.tag_name_attribute, @child[numeric_field.name] %>
66
</span>
77
<% if numeric_field.help_text.present?%>
8-
<br />
9-
<span class='help-text-container'>
10-
<span class='help-text'>
11-
<%=numeric_field.help_text%>
12-
</span>
13-
</span>
8+
<img class="tool-tip-icon vtip" title="<%=numeric_field.help_text%>" src="/images/tool_tip_icon.gif"/>
149
<% end%>
1510
</p>
+12-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
<% 5.times do |i|%>
12
<p class="file">
2-
<%= label_tag "child[photo]", "Photo" %>
3-
<%= file_field_tag "child[photo]" %>
3+
<%= label_tag "child[photo]#{i}", "Add Photo" %>
4+
<%= file_field_tag "child[photo]#{i}" %>
45
<% if photo_upload_box.help_text.present?%>
5-
<br />
6-
<span class='help-text-container'>
7-
<span class='help-text'>
8-
<%=photo_upload_box.help_text%>
9-
</span>
10-
</span>
6+
<img class="tool-tip-icon vtip" title="<%=photo_upload_box.help_text%>" src="/images/tool_tip_icon.gif"/>
117
<% end%>
128
</p>
13-
<% if !@child.id.nil? %>
14-
<div class="thumbnail"><%= thumbnail_tag(@child) %></div>
159
<% end %>
10+
11+
<% @child.photos.each do |photo| %>
12+
<p class="thumbnail">
13+
<%= label_tag "delete_child_photo[#{photo.name}]", "Delete photo?" %>
14+
<%= check_box_tag("delete_child_photo[#{photo.name}]") %>
15+
<%= thumbnail_tag(@child, photo.name) %>
16+
</p>
17+
<% end %>

app/views/children/_picture.html.erb

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<div class="profile-image">
2-
<div class="image">
3-
<%= link_to image_tag(child_resized_photo_path(@child, 328)), child_resized_photo_path(@child, 640) %>
2+
<% unless @child.photos.blank? %>
3+
<div class='image'>
4+
<%= link_to image_tag(child_resized_photo_path(@child, 328)), child_resized_photo_path(@child, 640) %>
5+
<p class='view-full-size'>Click on the Image to see full size</p>
6+
</div>
7+
<%= link_to("Edit photo", edit_photo_url(@child)) %>
8+
<% else %>
9+
<%= image_tag('/images/no_photo_clip.jpg') %>
10+
<% end %>
11+
</div>
412

5-
<p class="view-full-size">Click on the Image to see full size</p>
6-
</div>
7-
<%= link_to "Edit photo", edit_photo_url(@child) if @child.photo %>
13+
<div class="thumbnails">
14+
<% @child.photos.each do |photo| %>
15+
<div class="thumbnail"><%= thumbnail_tag(@child, photo.name) %></div>
16+
<% end %>
817
</div>

app/views/children/_radio_button.html.erb

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<dl class="radioList">
22
<dt><span><%= radio_button.name.humanize %></span>
33
<% if radio_button.help_text.present?%>
4-
<br/>
5-
<span class='help-text-container'>
6-
<span class='help-text'>
7-
<%=radio_button.help_text%>
8-
</span>
9-
</span>
4+
<img class="tool-tip-icon vtip" title="<%=radio_button.help_text%>" src="/images/tool_tip_icon.gif"/>
105
<% end%>
116
</dt>
127

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<p>
2-
<%= label_tag select_box.tag_name_attribute, select_box.display_name %>
3-
<%= select_tag select_box.tag_name_attribute, options_for_select(select_box.select_options, (@child[select_box.name] || '')) %>
2+
<%= label_tag select_box.tag_name_attribute, select_box.display_name %>
3+
<%= select_tag select_box.tag_name_attribute, options_for_select(select_box.select_options, (@child[select_box.name] || '')) %>
44
<% if select_box.help_text.present?%>
5-
<br />
6-
<span class='help-text-container'>
7-
<span class='help-text'>
8-
<%=select_box.help_text%>
9-
</span>
10-
</span>
5+
<img class="tool-tip-icon vtip" title="<%=select_box.help_text%>" src="/images/tool_tip_icon.gif"/>
116
<% end%>
127
</p>

0 commit comments

Comments
 (0)