Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored helpers

  • Loading branch information...
commit 94896f064a0076e343879c2f4583fec39e28124b 1 parent 9bd785a
@bbenezech bbenezech authored
Showing with 113 additions and 96 deletions.
  1. +0 −2  app/controllers/rails_admin/application_controller.rb
  2. +5 −5 app/controllers/rails_admin/main_controller.rb
  3. +26 −26 app/helpers/rails_admin/application_helper.rb
  4. +2 −2 app/views/layouts/rails_admin/_secondary_navigation.html.haml
  5. +1 −1  app/views/layouts/rails_admin/application.html.haml
  6. +1 −1  app/views/rails_admin/main/_form_filtering_multiselect.html.haml
  7. +2 −2 app/views/rails_admin/main/_form_filtering_select.html.haml
  8. +1 −1  app/views/rails_admin/main/dashboard.html.haml
  9. +2 −2 app/views/rails_admin/main/index.html.haml
  10. +3 −3 config/routes.rb
  11. +28 −15 lib/rails_admin/config/actions.rb
  12. +11 −6 lib/rails_admin/config/actions/base.rb
  13. +1 −1  lib/rails_admin/config/actions/bulk_delete.rb
  14. +1 −1  lib/rails_admin/config/actions/dashboard.rb
  15. +1 −1  lib/rails_admin/config/actions/delete.rb
  16. +1 −1  lib/rails_admin/config/actions/edit.rb
  17. +1 −1  lib/rails_admin/config/actions/export.rb
  18. +1 −1  lib/rails_admin/config/actions/history_index.rb
  19. +1 −1  lib/rails_admin/config/actions/history_show.rb
  20. +1 −1  lib/rails_admin/config/actions/index.rb
  21. +1 −1  lib/rails_admin/config/actions/new.rb
  22. +1 −1  lib/rails_admin/config/actions/show.rb
  23. +1 −1  lib/rails_admin/config/actions/show_in_app.rb
  24. +20 −19 spec/requests/authorization/cancan_spec.rb
View
2  app/controllers/rails_admin/application_controller.rb
@@ -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
View
10 app/controllers/rails_admin/main_controller.rb
@@ -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"
@@ -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]))
View
52 app/helpers/rails_admin/application_helper.rb
@@ -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
@@ -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),
@@ -42,35 +45,32 @@ 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>
}
@@ -78,14 +78,14 @@ def menu_for(parent, options = {}) # perf matters here (no action view trickery)
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
View
4 app/views/layouts/rails_admin/_secondary_navigation.html.haml
@@ -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
View
2  app/views/layouts/rails_admin/application.html.haml
@@ -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
View
2  app/views/rails_admin/main/_form_filtering_multiselect.html.haml
@@ -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
View
4 app/views/rails_admin/main/_form_filtering_select.html.haml
@@ -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
View
2  app/views/rails_admin/main/dashboard.html.haml
@@ -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
View
4 app/views/rails_admin/main/index.html.haml
@@ -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
@@ -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)
View
6 config/routes.rb
@@ -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
View
43 lib/rails_admin/config/actions.rb
@@ -3,7 +3,8 @@ module Config
module Actions
class << self
- def all(bindings)
+
+ def init_actions!
@@actions ||= [
Dashboard.new,
Index.new,
@@ -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
View
17 lib/rails_admin/config/actions/base.rb
@@ -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
@@ -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
View
2  lib/rails_admin/config/actions/bulk_delete.rb
@@ -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
View
2  lib/rails_admin/config/actions/dashboard.rb
@@ -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
View
2  lib/rails_admin/config/actions/delete.rb
@@ -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
View
2  lib/rails_admin/config/actions/edit.rb
@@ -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
View
2  lib/rails_admin/config/actions/export.rb
@@ -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
View
2  lib/rails_admin/config/actions/history_index.rb
@@ -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
View
2  lib/rails_admin/config/actions/history_show.rb
@@ -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
View
2  lib/rails_admin/config/actions/index.rb
@@ -4,7 +4,7 @@ module Actions
class Index < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
- register_instance_option :model_level do
+ register_instance_option :collection do
true
end
View
2  lib/rails_admin/config/actions/new.rb
@@ -4,7 +4,7 @@ module Actions
class New < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
- register_instance_option :model_level do
+ register_instance_option :collection do
true
end
View
2  lib/rails_admin/config/actions/show.rb
@@ -4,7 +4,7 @@ module Actions
class Show < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
- register_instance_option :object_level do
+ register_instance_option :member do
true
end
View
2  lib/rails_admin/config/actions/show_in_app.rb
@@ -4,7 +4,7 @@ module Actions
class ShowInApp < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
- register_instance_option :object_level do
+ register_instance_option :member do
true
end
View
39 spec/requests/authorization/cancan_spec.rb
@@ -77,14 +77,15 @@ def initialize(user)
]
visit index_path(:model_name => "player")
+
should have_content(@players[0].name)
should_not have_content(@players[1].name)
should_not have_content("Add new")
- should have_css('.show_object_link')
- should_not have_css('.edit_object_link')
- should_not have_css('.delete_object_link')
- should_not have_css('.history_show_object_link')
- should_not have_css('.show_in_app_object_link')
+ should have_css('.show_member_link')
+ should_not have_css('.edit_member_link')
+ should_not have_css('.delete_member_link')
+ should_not have_css('.history_show_member_link')
+ should_not have_css('.show_in_app_member_link')
end
it "GET /admin/team should raise CanCan::AccessDenied" do
@@ -174,10 +175,10 @@ def initialize(user)
@player = FactoryGirl.create :player
visit index_path(:model_name => "player")
- should have_css('.show_object_link')
- should_not have_css('.edit_object_link')
- should_not have_css('.delete_object_link')
- should have_css('.history_show_object_link')
+ should have_css('.show_member_link')
+ should_not have_css('.edit_member_link')
+ should_not have_css('.delete_member_link')
+ should have_css('.history_show_member_link')
visit show_path(:model_name => 'player', :id => @player.id)
should have_content("Show")
@@ -195,11 +196,11 @@ def initialize(user)
@player = FactoryGirl.create :player
visit index_path(:model_name => "player")
- should have_css('.show_object_link')
- should_not have_css('.edit_object_link')
- should_not have_css('.delete_object_link')
- should_not have_css('.history_show_object_link')
- should have_css('.show_in_app_object_link')
+ should have_css('.show_member_link')
+ should_not have_css('.edit_member_link')
+ should_not have_css('.delete_member_link')
+ should_not have_css('.history_show_member_link')
+ should have_css('.show_in_app_member_link')
visit show_path(:model_name => 'player', :id => @player.id)
should have_content("Show")
@@ -218,11 +219,11 @@ def initialize(user)
@player = FactoryGirl.create :player
visit index_path(:model_name => "player")
- should have_css('.show_object_link')
- should have_css('.edit_object_link')
- should have_css('.delete_object_link')
- should have_css('.history_show_object_link')
- should have_css('.show_in_app_object_link')
+ should have_css('.show_member_link')
+ should have_css('.edit_member_link')
+ should have_css('.delete_member_link')
+ should have_css('.history_show_member_link')
+ should have_css('.show_in_app_member_link')
visit show_path(:model_name => 'player', :id => @player.id)
should have_content("Show")
Please sign in to comment.
Something went wrong with that request. Please try again.