Skip to content

Commit

Permalink
refactored helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bbenezech committed Jan 19, 2012
1 parent 9bd785a commit 94896f0
Show file tree
Hide file tree
Showing 24 changed files with 113 additions and 96 deletions.
2 changes: 0 additions & 2 deletions app/controllers/rails_admin/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ def presented_model_name?(model_name)
!RailsAdmin::AbstractModel.lookup(model_name).nil?
end


private


def _get_plugin_name
@plugin_name_array ||= [RailsAdmin.config.main_app_name.is_a?(Proc) ? instance_eval(&RailsAdmin.config.main_app_name) : RailsAdmin.config.main_app_name].flatten
end
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/rails_admin/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ class MainController < RailsAdmin::ApplicationController

layout "rails_admin/application"

before_filter :get_model, :except => RailsAdmin::Config::Actions.root({}).map(&:action_name)
before_filter :get_object, :only => RailsAdmin::Config::Actions.object({}).map(&:action_name)
before_filter :get_model, :except => RailsAdmin::Config::Actions.all(:root).map(&:action_name)
before_filter :get_object, :only => RailsAdmin::Config::Actions.all(:member).map(&:action_name)
before_filter :check_for_cancel

RailsAdmin::Config::Actions.all({}).each do |action|
RailsAdmin::Config::Actions.all.each do |action|
class_eval %{
def #{action.action_name}
@action = RailsAdmin::Config::Actions.find('#{action.action_name}'.to_sym, {:controller => self, :abstract_model => @abstract_model, :object => @object})
@authorization_adapter.try(:authorize, *[@action.authorization_key, @abstract_model, @object].compact)
@authorization_adapter.try(:authorize, @action.authorization_key, @abstract_model, @object)
@page_name = wording_for(:title)
@page_type = @abstract_model && @abstract_model.pretty_name.downcase || "dashboard"
Expand All @@ -27,7 +27,7 @@ def #{action.action_name}
end

def bulk_action
self.send(params[:bulk_action]) if params[:bulk_action].in?(RailsAdmin::Config::Actions.all(:controller => self).select(&:bulkable?).map(&:route_fragment))
self.send(params[:bulk_action]) if params[:bulk_action].in?(RailsAdmin::Config::Actions.all(:controller => self, :abstract_model => @abstract_model).select(&:bulkable?).map(&:route_fragment))
end

def list_entries(model_config = @model_config, auth_scope_key = :index, additional_scope = get_association_scope_from_params, pagination = !(params[:associated_collection] || params[:all]))
Expand Down
52 changes: 26 additions & 26 deletions app/helpers/rails_admin/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ def current_action?(action)
@action.custom_key == action.custom_key
end

def action(key, abstract_model = nil, object = nil)
action = RailsAdmin::Config::Actions.find(key, { :controller => self.controller, :abstract_model => abstract_model, :object => object })
action && authorized?(action.authorization_key, (action.collection? || action.member?) && abstract_model || nil, action.member? && object || nil) ? action : nil
end

def actions(scope = :all, abstract_model = nil, object = nil)
RailsAdmin::Config::Actions.all(scope, { :controller => self.controller, :abstract_model => abstract_model, :object => object }).select do |action|
authorized?(action.authorization_key, (action.collection? || action.member?) && abstract_model || nil, action.member? && object || nil)
end
end

def edit_user_link
return nil unless authorized?(:edit, _current_user.class, _current_user) && _current_user.respond_to?(:email)
return nil unless abstract_model = RailsAdmin.config(_current_user.class).abstract_model
Expand All @@ -21,18 +32,10 @@ def edit_user_link
end


def wording_for(label, options = {})
action = case
when options[:action].nil?
@action
when options[:action].is_a?(Symbol) || options[:action].is_a?(String)
RailsAdmin::Config::Actions.find(options[:action].to_sym, { :controller => self.controller })
else
options[:action]
end
def wording_for(label, action = @action, abstract_model = @abstract_model, object = @object)

model_config = options[:model_config] || options[:abstract_model] && RailsAdmin.config(options[:abstract_model]) || @model_config
object = options[:object] || (model_config == @model_config) && @object || nil # don't use @object if model_config is not the current @model_config!
model_config = abstract_model && RailsAdmin.config(abstract_model)
action = RailsAdmin::Config::Actions.find(action.to_sym) if (action.is_a?(Symbol) || action.is_a?(String))

I18n.t("admin.actions.#{action.i18n_key}.#{label}",
:model_label => model_config.try(:label),
Expand All @@ -42,50 +45,47 @@ def wording_for(label, options = {})
end

def breadcrumb action = @action, acc = []

acc << content_tag(:li, :class => "#{"active" if current_action?(action)}") do
if action.http_methods.include?(:get) && authorized?(action.authorization_key, @abstract_model, @object)
link_to wording_for(:breadcrumb, :action => action), { :action => action.action_name, :controller => 'rails_admin/main' }
if action.http_methods.include?(:get)
link_to wording_for(:breadcrumb, action), { :action => action.action_name, :controller => 'rails_admin/main' }
else
content_tag(:span, wording_for(:breadcrumb, :action => action))
content_tag(:span, wording_for(:breadcrumb, action))
end
end

unless action.breadcrumb_parent # rec tail
unless action.breadcrumb_parent && (parent = action(action.breadcrumb_parent, @abstract_model, @object)) # rec tail
content_tag(:ul, :class => "breadcrumb") do
acc.reverse.join('<span class="divider">/</span>').html_safe
end
else
breadcrumb RailsAdmin::Config::Actions.find(action.breadcrumb_parent, { :controller => self.controller, :abstract_model => @abstract_model, :object => @object }), acc # rec
breadcrumb parent, acc # rec
end
end

# parent => :root, :model, :object
def menu_for(parent, options = {}) # perf matters here (no action view trickery)
abstract_model = options[:model_config].try(:abstract_model) || options[:abstract_model] || @abstract_model
object = options[:object] || (abstract_model == @abstract_model) && @object || nil # don't use @object if abstract_model is not the current @abstract_model!

actions = RailsAdmin::Config::Actions.send(parent, { :controller => self.controller, :abstract_model => abstract_model, :object => object }).select{ |action| action.http_methods.include?(:get) && authorized?(action.authorization_key, abstract_model, object) }

# parent => :root, :collection, :member
def menu_for(parent, abstract_model = nil, object = nil) # perf matters here (no action view trickery)
actions = actions(parent, abstract_model, object).select{ |action| action.http_methods.include?(:get) }
actions.map do |action|
%{
<li class="#{action.key}_#{parent}_link #{'active' if current_action?(action)}">
<a href="#{url_for({ :action => action.action_name, :controller => 'rails_admin/main', :model_name => abstract_model.try(:to_param), :id => object.try(:id) })}">
#{wording_for(:menu, options.merge(:action => action))}
#{wording_for(:menu, action)}
</a>
</li>
}
end.join.html_safe
end

def bulk_menu abstract_model = @abstract_model
actions = RailsAdmin::Config::Actions.all({ :controller => self.controller, :abstract_model => abstract_model }).select(&:bulkable?).select{ |action| authorized?(action.authorization_key, abstract_model) }
actions = actions(:bulkable, abstract_model)
return '' if actions.empty?
content_tag :li, { :class => 'dropdown', :style => 'float:right', :'data-dropdown' => "dropdown" } do
content_tag(:a, { :class => 'dropdown-toggle', :href => '#' }) { t('admin.misc.bulk_menu_title') } +
content_tag(:ul, :class => 'dropdown-menu') do
actions.map do |action|
content_tag :li do
link_to_function wording_for(:bulk_link, :action => action), "jQuery('#bulk_action').val('#{action.action_name}'); jQuery('#bulk_form').submit()"
link_to_function wording_for(:bulk_link, action), "jQuery('#bulk_action').val('#{action.action_name}'); jQuery('#bulk_form').submit()"
end
end.join.html_safe
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/rails_admin/_secondary_navigation.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- RailsAdmin::Config::Actions.root(:controller => self.controller).each do |action|
%li= link_to wording_for(:menu, :action => action), { :action => action.action_name, :controller => 'rails_admin/main' }
- actions(:root).each do |action|
%li= link_to wording_for(:menu, action), { :action => action.action_name, :controller => 'rails_admin/main' }
- if main_app_root_path = (main_app.root_path rescue false)
%li= link_to t('home.name').capitalize, main_app_root_path
- if _current_user
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/rails_admin/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
%p= value
= breadcrumb
%ul.tabs
= menu_for @abstract_model ? (@object.try(:id) ? :object : :model) : :root
= menu_for((@abstract_model ? (@object.try(:id) ? :member : :collection) : :root), @abstract_model, @object)
= content_for :contextual_tabs
%span.row
= yield
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
- if authorized? :new, config.abstract_model
- path_hash = { :model_name => config.abstract_model.to_param, :modal => true }
- path_hash.merge!({ :associations => { field.inverse_of => (form.object.id || 'new') } }) if field.inverse_of
= link_to wording_for(:link, :action => :new, :model_config => config), '#', :data => { :link => new_path(path_hash) }, :class => "create btn", :style => 'margin-left:10px'
= link_to wording_for(:link, :new, config.abstract_model), '#', :data => { :link => new_path(path_hash) }, :class => "create btn", :style => 'margin-left:10px'

= form.javascript_for(field) do
:plain
Expand Down
4 changes: 2 additions & 2 deletions app/views/rails_admin/main/_form_filtering_select.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
- if authorized? :new, config.abstract_model
- path_hash = { :model_name => config.abstract_model.to_param, :modal => true }
- path_hash.merge!({ :associations => { field.inverse_of => (form.object.id || 'new') } }) if field.inverse_of
= link_to wording_for(:link, :action => :new, :model_config => config), '#', :data => { :link => new_path(path_hash) }, :class => "btn create", :style => 'float:left; margin-left:10px'
= link_to wording_for(:link, :new, config.abstract_model), '#', :data => { :link => new_path(path_hash) }, :class => "btn create", :style => 'float:left; margin-left:10px'

- if edit_url.present?
= link_to wording_for(:link, :action => :edit, :model_config => config), '#', :data => { :link => edit_url }, :class => "btn update #{field.value.nil? && 'disabled'}", :style => 'float:left; margin-left:10px'
= link_to wording_for(:link, :edit, config.abstract_model), '#', :data => { :link => edit_url }, :class => "btn update #{field.value.nil? && 'disabled'}", :style => 'float:left; margin-left:10px'

= form.javascript_for(field) do
:plain
Expand Down
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/dashboard.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- percent = count > 0 ? (@max < 2.0 ? count : ((Math.log(count) * 100.0) / Math.log(@max))) : -1
.label.animate-width-to{:class => get_indicator(percent), :'data-animate-length' => ([1.0, percent].max.to_i * 20), :'data-animate-width-to' => "#{[2.0, percent].max.to_i}%", :style => "width:2%"}= @count[abstract_model.pretty_name]
%td.links
%ul.inline= menu_for :model, :abstract_model => abstract_model
%ul.inline= menu_for :collection, abstract_model
- if @auditing_adapter && authorized?(:history)
#block-tables.block
.content
Expand Down
4 changes: 2 additions & 2 deletions app/views/rails_admin/main/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
.span3
%button.btn.primary{:type => "submit", :'data-disable-with' => t("admin.misc.refresh")}= t("admin.misc.refresh")
- if export_action
%span{:style => 'float:right'}=link_to wording_for(:link, :action => export_action), export_path(params.except('set').except('page')), :class => 'btn info'
%span{:style => 'float:right'}=link_to wording_for(:link, export_action), export_path(params.except('set').except('page')), :class => 'btn info'

= form_tag bulk_action_path(:model_name => @abstract_model.to_param), :method => :post, :id => "bulk_form", :class => "form" do
= hidden_field_tag :bulk_action
Expand Down Expand Up @@ -113,7 +113,7 @@
- if @other_right_link ||= other_right && index_path(params.merge(:set => (params[:set].to_i + 1)))
%td.other.right= link_to "...", @other_right_link, :class => 'pjax'
%td.last.links
%ul.inline= menu_for :object, :object => object, :template => self
%ul.inline= menu_for :member, @abstract_model, object
- unless params[:all]
- total_count = @objects.total_count
= paginate(@objects, :theme => 'twitter-bootstrap', :remote => true)
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
RailsAdmin::Engine.routes.draw do
controller "main" do
RailsAdmin::Config::Actions.root({}).each { |action| match "/#{action.route_fragment}", :to => action.action_name, :as => action.action_name, :via => action.http_methods }
RailsAdmin::Config::Actions.all(:root).each { |action| match "/#{action.route_fragment}", :to => action.action_name, :as => action.action_name, :via => action.http_methods }
scope ":model_name" do
RailsAdmin::Config::Actions.model({}).each { |action| match "/#{action.route_fragment}", :to => action.action_name, :as => action.action_name, :via => action.http_methods }
RailsAdmin::Config::Actions.all(:collection).each { |action| match "/#{action.route_fragment}", :to => action.action_name, :as => action.action_name, :via => action.http_methods }
post "/bulk_action", :to => :bulk_action, :as => "bulk_action"
scope ":id" do
RailsAdmin::Config::Actions.object({}).each { |action| match "/#{action.route_fragment}", :to => action.action_name, :as => action.action_name, :via => action.http_methods }
RailsAdmin::Config::Actions.all(:member).each { |action| match "/#{action.route_fragment}", :to => action.action_name, :as => action.action_name, :via => action.http_methods }
end
end
end
Expand Down
43 changes: 28 additions & 15 deletions lib/rails_admin/config/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Config
module Actions

class << self
def all(bindings)

def init_actions!
@@actions ||= [
Dashboard.new,
Index.new,
Expand All @@ -17,25 +18,37 @@ def all(bindings)
HistoryIndex.new,
ShowInApp.new,
]
@@actions.map{ |action| action.with(bindings) }.select(&:visible?)
end

def find custom_key, bindings
all(bindings).find{ |a| a.custom_key == custom_key }
def all(scope = nil, bindings = {})
if scope.is_a?(Hash)
bindings = scope
scope = :all
end
scope ||= :all
init_actions!
actions = case scope
when :all
@@actions
when :root
@@actions.select(&:root?)
when :collection
@@actions.select(&:collection?)
when :bulkable
@@actions.select(&:bulkable?)
when :member
@@actions.select(&:member?)
end

bindings[:controller] ? actions.map{ |action| action.with(bindings) }.select(&:visible?) : actions
end

def root bindings
all(bindings).select &:root_level
end

def model bindings
all(bindings).select &:model_level
def find custom_key, bindings = {}
init_actions!
action = @@actions.find{ |a| a.custom_key == custom_key }
bindings[:controller] ? action && action.with(bindings).try(:visible?) && action : action
end

def object bindings
all(bindings).select &:object_level
end


def register(name, klass = nil)
if klass == nil && name.kind_of?(Class)
klass = name
Expand Down
17 changes: 11 additions & 6 deletions lib/rails_admin/config/actions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ module Actions
class Base < RailsAdmin::Config::Base
include RailsAdmin::Config::Hideable

# Should the action be visible
register_instance_option :visible? do
true
end

# Is the action acting on the root level (Example: /admin/contact)
register_instance_option :root_level? do
register_instance_option :root? do
false
end

# Is the action on a model scope (Example: /admin/teams/export)
register_instance_option :model_level? do
register_instance_option :collection? do
false
end

# Is the action on an object scope (Example: /admin/teams/1/edit)
register_instance_option :object_level? do
register_instance_option :member? do
false
end

Expand Down Expand Up @@ -75,11 +80,11 @@ class Base < RailsAdmin::Config::Base
# Breadcrumb parent
register_instance_option :breadcrumb_parent do
case
when root_level?
when root?
:dashboard
when model_level?
when collection?
:index
when object_level?
when member?
:show
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/bulk_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Actions
class BulkDelete < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)

register_instance_option :model_level do
register_instance_option :collection do
true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Actions
class Dashboard < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)

register_instance_option :root_level? do
register_instance_option :root? do
true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Actions
class Delete < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)

register_instance_option :object_level do
register_instance_option :member do
true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Actions
class Edit < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)

register_instance_option :object_level do
register_instance_option :member do
true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Actions
class Export < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)

register_instance_option :model_level do
register_instance_option :collection do
true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/history_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HistoryIndex < RailsAdmin::Config::Actions::Base
:history
end

register_instance_option :model_level do
register_instance_option :collection do
true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/history_show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HistoryShow < RailsAdmin::Config::Actions::Base
:history
end

register_instance_option :object_level do
register_instance_option :member do
true
end

Expand Down
Loading

0 comments on commit 94896f0

Please sign in to comment.