Permalink
Browse files

DRY up grouping records by date. Also you can now use the group_by_da…

…te method in your own plugins
  • Loading branch information...
djones committed Jul 2, 2010
1 parent 8368e51 commit 398d2b235372f6c054adc4386270db3a9d307d4e
@@ -18,12 +18,7 @@ def index
end
if RefinerySetting.find_or_set(:group_images_by_date_uploaded, true)
- @grouped_images = []
- @images.each do |image|
- key = image.created_at.strftime("%Y-%m-%d")
- image_group = @grouped_images.collect{|images| images.last if images.first == key }.flatten.compact << image
- (@grouped_images.delete_if {|i| i.first == key}) << [key, image_group]
- end
+ @grouped_images = group_by_date(@images)
end
end
@@ -24,18 +24,5 @@ def toggle_spam
def get_spam_count
@spam_count = Inquiry.count(:conditions => {:spam => true})
end
-
- # maybe move this up to the refinery level application controller as other plugins use a similar method
- def group_by_date(records)
- new_records = []
-
- records.each do |record|
- key = record.created_at.strftime("%Y-%m-%d")
- record_group = new_records.collect{|records| records.last if records.first == key }.flatten.compact << record
- (new_records.delete_if {|i| i.first == key}) << [key, record_group]
- end
-
- new_records
- end
end
@@ -37,7 +37,19 @@ def find_or_set_locale
I18n.locale = ::Refinery::I18n.current_locale
end
end
-
+
+ def group_by_date(records)
+ new_records = []
+
+ records.each do |record|
+ key = record.created_at.strftime("%Y-%m-%d")
+ record_group = new_records.collect{|records| records.last if records.first == key }.flatten.compact << record
+ (new_records.delete_if {|i| i.first == key}) << [key, record_group]
+ end
+
+ new_records
+ end
+
def restrict_plugins
Refinery::Plugins.set_active( current_user.authorized_plugins ) if current_user.respond_to? :plugins
end
@@ -44,12 +44,7 @@ def index
end
if RefinerySetting.find_or_set(:group_resources_by_date_uploaded, true)
- @grouped_resources = []
- @resources.each do |resource|
- key = resource.created_at.strftime("%Y-%m-%d")
- resource_group = @grouped_resources.collect{|resources| resources.last if resources.first == key }.flatten.compact << resource
- (@grouped_resources.delete_if {|i| i.first == key}) << [key, resource_group]
- end
+ @grouped_resources = group_by_date(@resources)
end
end

0 comments on commit 398d2b2

Please sign in to comment.