Skip to content

Commit

Permalink
Refactored DashboardHelper#activity_message_for to remove complexity.
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed Jul 3, 2012
1 parent 3bcc451 commit 9989ead
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions dashboard/app/helpers/refinery/admin/dashboard_helper.rb
Expand Up @@ -3,21 +3,34 @@ module Admin
module DashboardHelper

def activity_message_for(record)
if (plugin = Refinery::Plugins.active.find_by_model(record.class)) &&
(activity = plugin.activity_by_class_name(record.class.name).first)
# work out which action occured
action = record.updated_at.eql?(record.created_at) ? 'created' : 'updated'

# get article to define gender of model name, some languages require this for proper grammar
article = t('article', :scope => "refinery.plugins.#{plugin.name}.", :default => 'the')

# now create a link to the notification's corresponding record.
link_to t('.latest_activity_message',
:what => record.send(activity.title),
:kind => record.class.model_name.human,
:action => t("with_article \"#{article}\"", :scope => "refinery.#{action}")
).downcase.capitalize, eval("#{activity.url}(#{activity.nesting("record")})")
end
return if (plugin = find_plugin(record)).blank? || (activity = find_activity(record, plugin)).blank?

link_to t('.latest_activity_message',
:what => record.send(activity.title),
:kind => record.class.model_name.human,
:action => t("with_article \"#{plugin_article(plugin)}\"",
:scope => "refinery.#{record_action(record)}")
).downcase.capitalize, eval("#{activity.url}(#{activity.nesting('record')})")
end

private

def find_plugin(record)
Refinery::Plugins.active.find_by_model record.class
end

def find_activity(record, plugin = nil)
plugin ||= find_plugin(record) # avoid double lookup if we already have it
plugin.activity_by_class_name(record.class.name).first
end

def record_action(record)
record.updated_at.eql?(record.created_at) ? 'created' : 'updated'
end

# get article to define gender of model name, some languages require this for proper grammar
def plugin_article(plugin)
article = t('article', :scope => "refinery.plugins.#{plugin.name}.", :default => 'the')
end

end
Expand Down

0 comments on commit 9989ead

Please sign in to comment.