app/helpers/notification_renderer_helper.rb in notification-renderer-1.2.3 vs app/helpers/notification_renderer_helper.rb in notification-renderer-1.2.4

- old
+ new

@@ -1,28 +1,63 @@ module NotificationRendererHelper - def render_notification notification, renderer = NotificationRenderer.configuration.default_renderer, attribute = nil, notifications_count = nil + def render_notification notification, options = {} + defaults = { + renderer: NotificationRenderer.configuration.default_renderer, + attributes: nil, + notifications: nil + } + options = defaults.merge options + notification.update_attributes read: true if NotificationRenderer.configuration.auto_read - render "notifications/#{notification.type}/#{renderer}", notification: notification, attribute: attribute, notifications_count: notifications_count + render "notifications/#{notification.type}/#{options[:renderer]}", notification: notification, attributes: options[:attributes], notifications: options[:notifications] end - def render_notifications notifications, renderer = NotificationRenderer.configuration.default_renderer + def render_notifications notifications, options = {} + defaults = { + renderer: NotificationRenderer.configuration.default_renderer + } + options = defaults.merge options + content_tag :div, class: 'notification-renderer notifications' do notifications.each do |notification| - render_notification notification, renderer + render_notification notification, renderer: options[:renderer] end end end - def render_notifications_grouped notifications, group_by, renderer = NotificationRenderer.configuration.default_renderer + def render_notifications_grouped notifications, group_by, options = {} + defaults = { + renderer: NotificationRenderer.configuration.default_renderer, + group_by_date: false, + group_by_type: false + } + options = defaults.merge options + + group_by.unshift :type if options[:group_by_type] + group_by.unshift "created_at.beginning_of_#{options[:group_by_date]}" if options[:group_by_date] + content_tag :div, class: 'notification-renderer notifications' do - notifications.grouping(group_by)&.each do |attribute, notifications| - render_notification notifications.last, renderer, attribute, notifications.count - end + recursive_render_notifications_grouped notifications.grouping(options[:group_by]), options end end def notification_grouped? - local_assigns[:attribute] + local_assigns[:attributes] && local_assigns[:notifications] + end + + private + + def recursive_render_notifications_grouped notifications, i = 0, options = {} + options[:attributes] = {} unless options.has_key? :attributes + notifications.each_pair do |k, v| + options[:attributes][options[:group_by][i]] = k + i += 1 + if v.is_a? Hash + recursive_render_notifications_grouped v, options + else + render_notification v.last, renderer: options[:renderer], attributes: options[:attributes], notifications: v + end + end end end