From 828d9faeebe8db98f62f96a3e4a5ac4558c438a4 Mon Sep 17 00:00:00 2001 From: Kamil Dziemianowicz Date: Sat, 17 Jan 2015 21:41:22 +0100 Subject: [PATCH 01/16] Allow pagination to be controller with dynamic per_page --- .../active_admin/lib/per_page.js.coffee | 26 +++++++++++++++++++ .../active_admin/components/_pagination.scss | 10 +++++++ .../resource_controller/data_access.rb | 14 +++++++++- .../views/components/paginated_collection.rb | 17 ++++++++++++ lib/active_admin/views/pages/index.rb | 3 ++- .../components/paginated_collection_spec.rb | 16 ++++++++++++ 6 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/active_admin/lib/per_page.js.coffee diff --git a/app/assets/javascripts/active_admin/lib/per_page.js.coffee b/app/assets/javascripts/active_admin/lib/per_page.js.coffee new file mode 100644 index 00000000000..1a2f52d4c65 --- /dev/null +++ b/app/assets/javascripts/active_admin/lib/per_page.js.coffee @@ -0,0 +1,26 @@ +class ActiveAdmin.PerPage + constructor: (@options, @element)-> + @$element = $(@element) + @_init() + @_bind() + + _init: -> + @$params = @_queryParams() + + _bind: -> + @$element.change => + @$params['per_page'] = @$element.val() + location.search = $.param(@$params) + + _queryParams: -> + query = window.location.search.substring(1) + params = {} + re = /([^&=]+)=([^&]*)/g + while m = re.exec(query) + params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]) + params + +$.widget.bridge 'perPage', ActiveAdmin.PerPage + +$ -> + $('.pagination_per_page select').perPage() diff --git a/app/assets/stylesheets/active_admin/components/_pagination.scss b/app/assets/stylesheets/active_admin/components/_pagination.scss index f3fede99ef8..ad91730b128 100644 --- a/app/assets/stylesheets/active_admin/components/_pagination.scss +++ b/app/assets/stylesheets/active_admin/components/_pagination.scss @@ -32,3 +32,13 @@ .download_links { float: left; } + +.pagination_per_page { + float: right; + margin-left: 4px; + select { + @include light-button; + @include rounded(0px); + padding: 1px 5px; + } +} diff --git a/lib/active_admin/resource_controller/data_access.rb b/lib/active_admin/resource_controller/data_access.rb index b8fc7706774..596f6a6ee73 100644 --- a/lib/active_admin/resource_controller/data_access.rb +++ b/lib/active_admin/resource_controller/data_access.rb @@ -276,12 +276,24 @@ def apply_pagination(chain) def per_page if active_admin_config.paginate - @per_page || active_admin_config.per_page + dynamic_per_page || configured_per_page else max_per_page end end + def dynamic_per_page + params[:per_page] || @per_page + end + + def configured_per_page + if active_admin_config.per_page.is_a?(Array) + active_admin_config.per_page[0] + else + active_admin_config.per_page + end + end + def max_per_page 10_000 end diff --git a/lib/active_admin/views/components/paginated_collection.rb b/lib/active_admin/views/components/paginated_collection.rb index dd07031add0..598ad14e2a1 100644 --- a/lib/active_admin/views/components/paginated_collection.rb +++ b/lib/active_admin/views/components/paginated_collection.rb @@ -40,6 +40,7 @@ def build(collection, options = {}) @param_name = options.delete(:param_name) @download_links = options.delete(:download_links) @display_total = options.delete(:pagination_total) { true } + @per_page = options.delete(:per_page) unless collection.respond_to?(:num_pages) raise(StandardError, "Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.") @@ -63,6 +64,7 @@ def add_child(*args, &block) def build_pagination_with_formats(options) div id: "index_footer" do + build_per_page_select if @per_page.is_a?(Array) build_pagination div(page_entries_info(options).html_safe, class: "pagination_information") @@ -76,6 +78,21 @@ def build_pagination_with_formats(options) end end + def build_per_page_select + div class: "pagination_per_page" do + text_node "Per page:" + select do + @per_page.each do |per_page| + option( + per_page, + value: per_page, + selected: collection.limit_value == per_page ? "selected" : nil + ) + end + end + end + end + def build_pagination options = {} options[:param_name] = @param_name if @param_name diff --git a/lib/active_admin/views/pages/index.rb b/lib/active_admin/views/pages/index.rb index a53427ab925..749b2f26459 100644 --- a/lib/active_admin/views/pages/index.rb +++ b/lib/active_admin/views/pages/index.rb @@ -127,11 +127,13 @@ def render_index paginator = config.fetch(:paginator, true) download_links = config.fetch(:download_links, active_admin_config.namespace.download_links) pagination_total = config.fetch(:pagination_total, true) + per_page = config.fetch(:per_page, active_admin_config.per_page) paginated_collection(collection, entry_name: active_admin_config.resource_label, entries_name: active_admin_config.plural_resource_label(count: collection_size), download_links: download_links, paginator: paginator, + per_page: per_page, pagination_total: pagination_total) do div class: 'index_content' do insert_tag(renderer_class, config, collection) @@ -159,4 +161,3 @@ def default_blank_slate_link end end end - diff --git a/spec/unit/views/components/paginated_collection_spec.rb b/spec/unit/views/components/paginated_collection_spec.rb index 9a08d96c7ef..12e94ed9d1f 100644 --- a/spec/unit/views/components/paginated_collection_spec.rb +++ b/spec/unit/views/components/paginated_collection_spec.rb @@ -216,5 +216,21 @@ def paginated_collection(*args) end end + context "when specifying per_page: array option" do + let(:collection) do + posts = 10.times.map { Post.new } + Kaminari.paginate_array(posts).page(1).per(5) + end + + let(:pagination) { paginated_collection(collection, per_page: [1, 2, 3]) } + let(:pagination_html) { pagination.find_by_class("pagination_per_page").first } + let(:pagination_node) { Capybara.string(pagination_html.to_s) } + + it "should render per_page select tag" do + expect(pagination_html.content).to match(/Per page:/) + expect(pagination_node).to have_css("select option", count: 3) + end + end + end end From 2b114a14ad6409b2df402bab00afa91939b5a304 Mon Sep 17 00:00:00 2001 From: Igor Fedoronchuk Date: Sun, 18 Jan 2015 16:00:14 +0200 Subject: [PATCH 02/16] remove param from uri when changing per_page --- app/assets/javascripts/active_admin/lib/per_page.js.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/app/assets/javascripts/active_admin/lib/per_page.js.coffee b/app/assets/javascripts/active_admin/lib/per_page.js.coffee index 1a2f52d4c65..3bd29d2c341 100644 --- a/app/assets/javascripts/active_admin/lib/per_page.js.coffee +++ b/app/assets/javascripts/active_admin/lib/per_page.js.coffee @@ -10,6 +10,7 @@ class ActiveAdmin.PerPage _bind: -> @$element.change => @$params['per_page'] = @$element.val() + delete @$params['page'] location.search = $.param(@$params) _queryParams: -> From bbaacb878a730728d81aae1b9e521268dbceed03 Mon Sep 17 00:00:00 2001 From: Luciano Sousa Date: Sun, 18 Jan 2015 14:06:36 -0300 Subject: [PATCH 03/16] improve documentation add missing block variable --- docs/6-show-pages.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/6-show-pages.md b/docs/6-show-pages.md index 86401480e12..07c3777fcdc 100644 --- a/docs/6-show-pages.md +++ b/docs/6-show-pages.md @@ -33,7 +33,7 @@ ActiveAdmin.register Ad do show do attributes_table do row :title - row :image do + row :image do |ad| image_tag ad.image.url end end From d7a00727e3c17802fc7e12601ca799f5e4a9aeee Mon Sep 17 00:00:00 2001 From: Timo Schilling Date: Mon, 20 Oct 2014 23:04:55 +0200 Subject: [PATCH 04/16] change exec context for batch_action form option fix for https://github.com/activeadmin/activeadmin/pull/1815#issuecomment-59659670 --- lib/active_admin/batch_actions/controller.rb | 2 +- lib/active_admin/batch_actions/resource_extension.rb | 3 +-- lib/active_admin/batch_actions/views/batch_action_selector.rb | 2 +- spec/unit/resource_controller_spec.rb | 1 + 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/active_admin/batch_actions/controller.rb b/lib/active_admin/batch_actions/controller.rb index aba8b2b3ffe..cc500f5824a 100644 --- a/lib/active_admin/batch_actions/controller.rb +++ b/lib/active_admin/batch_actions/controller.rb @@ -7,7 +7,7 @@ def batch_action if action_present? selection = params[:collection_selection] || [] inputs = JSON.parse params[:batch_action_inputs] || '{}' - inputs = inputs.with_indifferent_access.slice *current_batch_action.inputs.keys + inputs = inputs.with_indifferent_access.slice *instance_exec(¤t_batch_action.inputs).keys instance_exec selection, inputs, ¤t_batch_action.block else raise "Couldn't find batch action \"#{params[:batch_action]}\"" diff --git a/lib/active_admin/batch_actions/resource_extension.rb b/lib/active_admin/batch_actions/resource_extension.rb index f570ffce2d1..634b9f7b17a 100644 --- a/lib/active_admin/batch_actions/resource_extension.rb +++ b/lib/active_admin/batch_actions/resource_extension.rb @@ -129,8 +129,7 @@ def confirm end def inputs - HashWithIndifferentAccess.new \ - @options[:form].is_a?(Proc) ? @options[:form].call : @options[:form] + @options[:form].is_a?(Proc) ? @options[:form] : proc { @options[:form] } end # Returns the display if block. If the block was not explicitly defined diff --git a/lib/active_admin/batch_actions/views/batch_action_selector.rb b/lib/active_admin/batch_actions/views/batch_action_selector.rb index 136cc06af25..fe070acebae 100644 --- a/lib/active_admin/batch_actions/views/batch_action_selector.rb +++ b/lib/active_admin/batch_actions/views/batch_action_selector.rb @@ -33,7 +33,7 @@ def build_drop_down :class => "batch_action", "data-action" => batch_action.sym, "data-confirm" => confirmation_text, - "data-inputs" => batch_action.inputs.to_json + "data-inputs" => instance_exec(batch_action.inputs).to_json } default_title = render_or_call_method_or_proc_on(self, batch_action.title) diff --git a/spec/unit/resource_controller_spec.rb b/spec/unit/resource_controller_spec.rb index e04fa5682e7..0b1f3a835f3 100644 --- a/spec/unit/resource_controller_spec.rb +++ b/spec/unit/resource_controller_spec.rb @@ -233,6 +233,7 @@ def call_after_destroy(obj); end describe "when params batch_action matches existing BatchAction" do it "should call the block with args" do allow(controller).to receive(:params) { { batch_action: "flag", collection_selection: ["1"] } } + expect(controller).to receive(:instance_exec).with(no_args).and_return({}) expect(controller).to receive(:instance_exec).with(["1"], {}) controller.batch_action end From f3482f1ee05dff775d05ff599f7d670eb804e40d Mon Sep 17 00:00:00 2001 From: Igor Fedoronchuk Date: Mon, 19 Jan 2015 15:32:15 +0200 Subject: [PATCH 05/16] use render_in_context for eval form block --- lib/active_admin/batch_actions/controller.rb | 7 ++++--- lib/active_admin/batch_actions/resource_extension.rb | 2 +- .../batch_actions/views/batch_action_selector.rb | 2 +- spec/unit/resource_controller_spec.rb | 11 +++++++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/active_admin/batch_actions/controller.rb b/lib/active_admin/batch_actions/controller.rb index cc500f5824a..fe8b27105e1 100644 --- a/lib/active_admin/batch_actions/controller.rb +++ b/lib/active_admin/batch_actions/controller.rb @@ -5,9 +5,10 @@ module Controller # Controller action that is called when submitting the batch action form def batch_action if action_present? - selection = params[:collection_selection] || [] - inputs = JSON.parse params[:batch_action_inputs] || '{}' - inputs = inputs.with_indifferent_access.slice *instance_exec(¤t_batch_action.inputs).keys + selection = params[:collection_selection] || [] + inputs = JSON.parse params[:batch_action_inputs] || '{}' + valid_keys = render_in_context(self, current_batch_action.inputs).try(:keys) + inputs = inputs.with_indifferent_access.slice *valid_keys instance_exec selection, inputs, ¤t_batch_action.block else raise "Couldn't find batch action \"#{params[:batch_action]}\"" diff --git a/lib/active_admin/batch_actions/resource_extension.rb b/lib/active_admin/batch_actions/resource_extension.rb index 634b9f7b17a..1a2904f679c 100644 --- a/lib/active_admin/batch_actions/resource_extension.rb +++ b/lib/active_admin/batch_actions/resource_extension.rb @@ -129,7 +129,7 @@ def confirm end def inputs - @options[:form].is_a?(Proc) ? @options[:form] : proc { @options[:form] } + @options[:form] end # Returns the display if block. If the block was not explicitly defined diff --git a/lib/active_admin/batch_actions/views/batch_action_selector.rb b/lib/active_admin/batch_actions/views/batch_action_selector.rb index fe070acebae..cf370812141 100644 --- a/lib/active_admin/batch_actions/views/batch_action_selector.rb +++ b/lib/active_admin/batch_actions/views/batch_action_selector.rb @@ -33,7 +33,7 @@ def build_drop_down :class => "batch_action", "data-action" => batch_action.sym, "data-confirm" => confirmation_text, - "data-inputs" => instance_exec(batch_action.inputs).to_json + "data-inputs" => render_in_context(self, batch_action.inputs).to_json } default_title = render_or_call_method_or_proc_on(self, batch_action.title) diff --git a/spec/unit/resource_controller_spec.rb b/spec/unit/resource_controller_spec.rb index 0b1f3a835f3..c2c3d351b14 100644 --- a/spec/unit/resource_controller_spec.rb +++ b/spec/unit/resource_controller_spec.rb @@ -231,12 +231,19 @@ def call_after_destroy(obj); end end describe "when params batch_action matches existing BatchAction" do - it "should call the block with args" do + before do allow(controller).to receive(:params) { { batch_action: "flag", collection_selection: ["1"] } } - expect(controller).to receive(:instance_exec).with(no_args).and_return({}) + end + + it "should call the block with args" do expect(controller).to receive(:instance_exec).with(["1"], {}) controller.batch_action end + + it "should call the block in controller scope" do + expect(controller).to receive(:render_in_context).with(controller, nil).and_return({}) + controller.batch_action + end end describe "when params batch_action doesn't match a BatchAction" do From 649b400575dcce2f49d302e5dd4f0cbe74e93796 Mon Sep 17 00:00:00 2001 From: Dan Kohn Date: Tue, 20 Jan 2015 10:44:50 -0500 Subject: [PATCH 06/16] Rename print.css.scss to print.scss --- .../stylesheets/active_admin/{print.css.scss => print.scss} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/assets/stylesheets/active_admin/{print.css.scss => print.scss} (100%) diff --git a/app/assets/stylesheets/active_admin/print.css.scss b/app/assets/stylesheets/active_admin/print.scss similarity index 100% rename from app/assets/stylesheets/active_admin/print.css.scss rename to app/assets/stylesheets/active_admin/print.scss From 2ece85578fd18da50647fc944a5f737b89872c8c Mon Sep 17 00:00:00 2001 From: Andrei Latchescu Date: Wed, 21 Jan 2015 17:06:04 +0200 Subject: [PATCH 07/16] Fixed some JS initializers to run on turbolinks page:load also --- app/assets/javascripts/active_admin/application.js.coffee | 2 +- app/assets/javascripts/active_admin/lib/batch_actions.js.coffee | 2 +- app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/active_admin/application.js.coffee b/app/assets/javascripts/active_admin/application.js.coffee index 8fa2a135bd0..530adf2692e 100644 --- a/app/assets/javascripts/active_admin/application.js.coffee +++ b/app/assets/javascripts/active_admin/application.js.coffee @@ -1,5 +1,5 @@ # Initializers -$ -> +$(document).on 'ready page:load', -> # jQuery datepickers (also evaluates dynamically added HTML) $(document).on 'focus', '.datepicker:not(.hasDatepicker)', -> defaults = dateFormat: 'yy-mm-dd' diff --git a/app/assets/javascripts/active_admin/lib/batch_actions.js.coffee b/app/assets/javascripts/active_admin/lib/batch_actions.js.coffee index a6cf08e6d74..42013a07874 100644 --- a/app/assets/javascripts/active_admin/lib/batch_actions.js.coffee +++ b/app/assets/javascripts/active_admin/lib/batch_actions.js.coffee @@ -1,4 +1,4 @@ -$ -> +$(document).on 'ready page:load', -> # # Use ActiveAdmin.modal_dialog to prompt user if confirmation is required for current Batch Action diff --git a/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee b/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee index e997fb6bb6a..bd3920706ea 100644 --- a/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee +++ b/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee @@ -97,5 +97,5 @@ class ActiveAdmin.DropdownMenu $.widget.bridge 'aaDropdownMenu', ActiveAdmin.DropdownMenu -$ -> +$(document).on 'ready page:load', -> $('.dropdown_menu').aaDropdownMenu() From 156926edf393251e954ff46946acb32bae3911c0 Mon Sep 17 00:00:00 2001 From: Pranas Kiziela Date: Tue, 9 Dec 2014 16:50:40 +0200 Subject: [PATCH 08/16] Do not auto link to inaccessible actions --- CHANGELOG.md | 2 + lib/active_admin/namespace.rb | 4 +- lib/active_admin/resource/routes.rb | 20 +++++++++- .../view_helpers/auto_link_helper.rb | 9 ++++- lib/active_admin/views/tabbed_navigation.rb | 6 ++- spec/unit/auto_link_spec.rb | 39 ++++++++++++++++++- spec/unit/views/tabbed_navigation_spec.rb | 9 +++++ 7 files changed, 81 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9741f15c033..404bf04a085 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ #### Minor +* Do not auto link to inaccessible actions [#3686][] by [@pranas][] * Allow to enable comments on per-resource basis [#3695][] by [@pranas][] * Unify DSL for index `actions` and `actions dropdown: true` [#3463][] by [@timoschilling][] * Add DSL method `includes` for `ActiveRecord::Relation#includes` [#3464][] by [@timoschilling][] @@ -989,6 +990,7 @@ of the highlights. 250 commits. Enough said. [#3486]: https://github.com/activeadmin/activeadmin/issues/3486 [#3519]: https://github.com/activeadmin/activeadmin/issues/3519 [#3606]: https://github.com/activeadmin/activeadmin/issues/3606 +[#3686]: https://github.com/activeadmin/activeadmin/issues/3686 [#3695]: https://github.com/activeadmin/activeadmin/issues/3695 [#3731]: https://github.com/activeadmin/activeadmin/issues/3731 [@Bishop]: https://github.com/Bishop diff --git a/lib/active_admin/namespace.rb b/lib/active_admin/namespace.rb index 6aa75a8ff0a..18796b2f6ee 100644 --- a/lib/active_admin/namespace.rb +++ b/lib/active_admin/namespace.rb @@ -146,8 +146,8 @@ def add_logout_button_to_menu(menu, priority = 20, html_options = {}) def add_current_user_to_menu(menu, priority = 10, html_options = {}) if current_user_method menu.add id: 'current_user', priority: priority, html_options: html_options, - label: ->{ display_name current_active_admin_user }, - url: ->{ auto_url_for(current_active_admin_user) || '#' }, + label: -> { display_name current_active_admin_user }, + url: -> { auto_url_for(current_active_admin_user) }, if: :current_active_admin_user? end end diff --git a/lib/active_admin/resource/routes.rb b/lib/active_admin/resource/routes.rb index 1ec928a8f7d..6330ed735d7 100644 --- a/lib/active_admin/resource/routes.rb +++ b/lib/active_admin/resource/routes.rb @@ -15,6 +15,10 @@ def route_instance_path(resource) RouteBuilder.new(self).instance_path(resource) end + def route_edit_instance_path(resource) + RouteBuilder.new(self).edit_instance_path(resource) + end + # Returns the routes prefix for this config def route_prefix namespace.module_name.try(:underscore) @@ -36,7 +40,7 @@ def initialize(resource) def collection_path(params) route_name = route_name( resource.resources_configuration[:self][:route_collection_name], - (resource.route_uncountable? ? 'index_path' : 'path') + suffix: (resource.route_uncountable? ? "index_path" : "path") ) routes.public_send route_name, *route_collection_params(params) @@ -51,13 +55,25 @@ def instance_path(instance) routes.public_send route_name, *route_instance_params(instance) end + # @return [String] the path to the edit page of this resource + # @param instance [ActiveRecord::Base] the instance we want the path of + # @example "/admin/posts/1/edit" + def edit_instance_path(instance) + path = resource.resources_configuration[:self][:route_instance_name] + route_name = route_name(path, action: :edit) + + routes.public_send route_name, *route_instance_params(instance) + end + private attr_reader :resource - def route_name(resource_path_name, suffix = 'path') + def route_name(resource_path_name, options = {}) + suffix = options[:suffix] || "path" route = [] + route << options[:action] # "edit" or "new" route << resource.route_prefix # "admin" route << belongs_to_name if nested? # "category" route << resource_path_name # "posts" or "post" diff --git a/lib/active_admin/view_helpers/auto_link_helper.rb b/lib/active_admin/view_helpers/auto_link_helper.rb index 46fa618eb30..69935a6f6fd 100644 --- a/lib/active_admin/view_helpers/auto_link_helper.rb +++ b/lib/active_admin/view_helpers/auto_link_helper.rb @@ -21,8 +21,15 @@ def auto_link(resource, content = display_name(resource)) # Like `auto_link`, except that it only returns a URL instead of a full tag def auto_url_for(resource) - if config = active_admin_resource_for(resource.class) + config = active_admin_resource_for(resource.class) + return unless config + + if config.controller.action_methods.include?("show") && + authorized?(ActiveAdmin::Auth::READ, resource) url_for config.route_instance_path resource + elsif config.controller.action_methods.include?("edit") && + authorized?(ActiveAdmin::Auth::UPDATE, resource) + url_for config.route_edit_instance_path resource end end diff --git a/lib/active_admin/views/tabbed_navigation.rb b/lib/active_admin/views/tabbed_navigation.rb index 9b5b18f7fef..f72c838d623 100644 --- a/lib/active_admin/views/tabbed_navigation.rb +++ b/lib/active_admin/views/tabbed_navigation.rb @@ -43,7 +43,11 @@ def build_menu_item(item) li id: item.id do |li| li.add_class "current" if item.current? assigns[:current_tab] - text_node link_to item.label(self), item.url(self), item.html_options + if url = item.url(self) + text_node link_to item.label(self), url, item.html_options + else + span item.label(self), item.html_options + end if children = item.items(self).presence li.add_class "has_nested" diff --git a/spec/unit/auto_link_spec.rb b/spec/unit/auto_link_spec.rb index f34cd992f25..822efc0b1eb 100644 --- a/spec/unit/auto_link_spec.rb +++ b/spec/unit/auto_link_spec.rb @@ -14,6 +14,10 @@ def admin_post_path(post) "/admin/posts/#{post.id}" end + def authorized?(_action, _subject) + true + end + context "when the resource is not registered" do it "should return the display name of the object" do expect(auto_link(post)).to eq "Hello World" @@ -25,10 +29,41 @@ def admin_post_path(post) active_admin_namespace.register Post end it "should return a link with the display name of the object" do - expect(self).to receive(:url_for).and_return admin_post_path(post) + expect(self).to receive(:url_for) { |url| url } expect(self).to receive(:link_to).with "Hello World", admin_post_path(post) auto_link(post) end - end + context "but the user doesn't have access" do + it "should return the display name of the object" do + expect(self).to receive(:authorized?).twice.and_return(false) + expect(auto_link(post)).to eq "Hello World" + end + end + + context "but the show action is disabled" do + before do + active_admin_namespace.register(Post) { actions :all, except: :show } + end + + it "should fallback to edit" do + url_path = "/admin/posts/#{post.id}/edit" + expect(self).to receive(:url_for) { |url| url } + expect(self).to receive(:link_to).with "Hello World", url_path + auto_link(post) + end + end + + context "but the show and edit actions are disabled" do + before do + active_admin_namespace.register(Post) do + actions :all, except: [:show, :edit] + end + end + + it "should return the display name of the object" do + expect(auto_link(post)).to eq "Hello World" + end + end + end end diff --git a/spec/unit/views/tabbed_navigation_spec.rb b/spec/unit/views/tabbed_navigation_spec.rb index f2cf7a838e3..e52556fbd08 100644 --- a/spec/unit/views/tabbed_navigation_spec.rb +++ b/spec/unit/views/tabbed_navigation_spec.rb @@ -48,6 +48,8 @@ priority: 10, if: :admin_logged_in? end + + menu.add label: "Charles Smith", id: "current_user", url: -> { nil } end it "should generate a ul" do @@ -95,6 +97,13 @@ expect(html).to_not have_selector("a[href='#']", text: "Management") end + context "when url is nil" do + it "should generate a span" do + selector = "li#current_user > span" + expect(html).to have_selector(selector, text: "Charles Smith") + end + end + describe "marking current item" do it "should add the 'current' class to the li" do From 6293d9e0d1348893077232baf6590767c3b6c415 Mon Sep 17 00:00:00 2001 From: Timo Schilling Date: Wed, 21 Jan 2015 18:42:29 +0100 Subject: [PATCH 09/16] change inherited_resources to 1.6 to support rails 4.2 --- activeadmin.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activeadmin.gemspec b/activeadmin.gemspec index 22d98a0e834..f81f6ead74b 100644 --- a/activeadmin.gemspec +++ b/activeadmin.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.add_dependency 'coffee-rails' s.add_dependency 'formtastic', '~> 3.1' s.add_dependency 'formtastic_i18n' - s.add_dependency 'inherited_resources', '~> 1.4', '!= 1.5.0' + s.add_dependency 'inherited_resources', '~> 1.6' s.add_dependency 'jquery-rails' s.add_dependency 'jquery-ui-rails', '~> 5.0' s.add_dependency 'kaminari', '~> 0.15' From e4c7b9011217016f1451e250acfddc49fa85163a Mon Sep 17 00:00:00 2001 From: Timo Schilling Date: Wed, 21 Jan 2015 19:16:18 +0100 Subject: [PATCH 10/16] remove rails 4.2 section from readme it's no longer needed since inherited_resources 1.6 is released --- README.md | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/README.md b/README.md index 7a813c1cd88..bbf8b22968d 100644 --- a/README.md +++ b/README.md @@ -22,18 +22,6 @@ You can get Rails 4.x support by tracking master: gem 'activeadmin', github: 'activeadmin' ``` -#### Rails 4.2 - -To use ActiveAdmin with Rails 4.2, you need to change your Gemfile like this: - -```ruby -gem 'activeadmin', github: 'activeadmin' -gem 'inherited_resources', github: 'josevalim/inherited_resources', branch: 'rails-4-2' -``` - -*But keep in mind that `inherited_resources` still don't support Rails 4.2 officially.* -*Track [josevalim/inherited_resources#381](https://github.com/josevalim/inherited_resources/issues/381) for details.* - ### 0.6.x The plan is to follow [semantic versioning](http://semver.org/) as of 1.0.0. The 0.6.x line will From 9490ca3ff19339240af9992aece8d6e24d600ebb Mon Sep 17 00:00:00 2001 From: Timo Schilling Date: Wed, 21 Jan 2015 19:28:48 +0100 Subject: [PATCH 11/16] use inherited_resources from rubygems --- Gemfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Gemfile b/Gemfile index 08f65a686df..76ac76250fb 100644 --- a/Gemfile +++ b/Gemfile @@ -7,8 +7,6 @@ require File.expand_path 'spec/support/detect_rails_version', File.dirname(__FIL rails_version = detect_rails_version gem 'rails', rails_version -gem 'inherited_resources', github: 'josevalim/inherited_resources', branch: 'rails-4-2' - # Optional dependencies gem 'cancan' gem 'devise' From 35420017a3a07ed45c262ff71ba0c76431a2f643 Mon Sep 17 00:00:00 2001 From: John Maxwell Date: Mon, 26 Jan 2015 18:16:02 -0500 Subject: [PATCH 12/16] Fixes #3583: Associates comments with STI base classes The Rails convention of a polymorphic belongs_to is to associate with the base class not the subclass. The makes the association more durable, in the case that a model instance changes its type (switches subclasses). In addition, all comments through 0.6.x were associated with the resource base class. The change to associate with the subclass has orphaned those Comments. This change fixes those problems. --- features/comments/commenting.feature | 2 -- lib/active_admin/orm/active_record/comments/comment.rb | 2 +- spec/unit/comments_spec.rb | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/features/comments/commenting.feature b/features/comments/commenting.feature index b689d0a8e66..de011d8bca2 100644 --- a/features/comments/commenting.feature +++ b/features/comments/commenting.feature @@ -143,8 +143,6 @@ Feature: Commenting When I add a comment "Hello World" Then I should see a flash with "Comment was successfully created" And I should be in the resource section for publishers - When I am on the index page for comments - Then I should see the content "Publisher" And I should see "Hello World" Scenario: Commenting on a class with string id diff --git a/lib/active_admin/orm/active_record/comments/comment.rb b/lib/active_admin/orm/active_record/comments/comment.rb index d8d28587c71..22ef7f3f418 100644 --- a/lib/active_admin/orm/active_record/comments/comment.rb +++ b/lib/active_admin/orm/active_record/comments/comment.rb @@ -16,7 +16,7 @@ class Comment < ActiveRecord::Base # @return [String] The name of the record to use for the polymorphic relationship def self.resource_type(resource) - ResourceController::Decorators.undecorate(resource).class.name.to_s + ResourceController::Decorators.undecorate(resource).class.base_class.name.to_s end # Postgres adapters won't compare strings to numbers (issue 34) diff --git a/spec/unit/comments_spec.rb b/spec/unit/comments_spec.rb index 5097e6cf0a1..460beceb24f 100644 --- a/spec/unit/comments_spec.rb +++ b/spec/unit/comments_spec.rb @@ -126,7 +126,7 @@ namespace: namespace_name) expect(ActiveAdmin::Comment.find_for_resource_in_namespace(publisher, namespace_name).last.resource_type). - to eq('Publisher') + to eq("User") end end end From cede794d69229ed7f409544485ead824fb0ffaba Mon Sep 17 00:00:00 2001 From: Michael Yagudaev Date: Mon, 26 Jan 2015 19:57:41 -0800 Subject: [PATCH 13/16] Update 1-general-configuration.md Demonstrate dynamically setting the `site_title_image`, this is great for multi-tenant use (which is exactly how I discovered this by digging through the code and trying it out). --- docs/1-general-configuration.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/1-general-configuration.md b/docs/1-general-configuration.md index 5fda24c5933..4e55749406a 100644 --- a/docs/1-general-configuration.md +++ b/docs/1-general-configuration.md @@ -37,6 +37,7 @@ config.site_title = "My Admin Site" config.site_title_link = "/" config.site_title_image = "site_image.png" config.site_title_image = "http://www.google.com/images/logos/google_logo_41.png" +config.site_title_image = ->(context) { context.current_user.company.logo_url } ``` ## Internationalization (I18n) From fe7ccc896dd09bced597a7eb436e72284cd46131 Mon Sep 17 00:00:00 2001 From: Igor Fedoronchuk Date: Wed, 28 Jan 2015 23:00:17 +0200 Subject: [PATCH 14/16] allow tabs to be used in sidebar --- app/assets/javascripts/active_admin/application.js.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/active_admin/application.js.coffee b/app/assets/javascripts/active_admin/application.js.coffee index 530adf2692e..0a19718c7bb 100644 --- a/app/assets/javascripts/active_admin/application.js.coffee +++ b/app/assets/javascripts/active_admin/application.js.coffee @@ -24,8 +24,8 @@ $(document).on 'ready page:load', -> $('.filter_form_field.select_and_search select').change -> $(@).siblings('input').prop name: "q[#{@value}]" - # Tab navigation in the show page - $('#main_content .tabs').tabs() + # Tab navigation + $('#active_admin_content .tabs').tabs() # In order for index scopes to overflow properly onto the next line, we have # to manually set its width based on the width of the batch action button. From 782d7e4f8dbf0d8c2d223ffb963638bb4b9455bc Mon Sep 17 00:00:00 2001 From: Aaron Sakowski Date: Thu, 29 Jan 2015 17:01:29 -0800 Subject: [PATCH 15/16] fix to prevent coffeescript from misinterpreting --- .../javascripts/active_admin/lib/checkbox-toggler.js.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee b/app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee index 5ab2dccefdf..57be96e4f78 100644 --- a/app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee +++ b/app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee @@ -1,7 +1,7 @@ class ActiveAdmin.CheckboxToggler constructor: (@options, @container)-> defaults = {} - @options = $.extend defaults, options + @options = $.extend defaults, @options @_init() @_bind() From 553e9888f1c177dec2f5f4fa2e922ab6e832c8a4 Mon Sep 17 00:00:00 2001 From: Aaron Sakowski Date: Thu, 29 Jan 2015 17:27:35 -0800 Subject: [PATCH 16/16] fixed another ambiguity --- app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee b/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee index bd3920706ea..446beb2debd 100644 --- a/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee +++ b/app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee @@ -9,7 +9,7 @@ class ActiveAdmin.DropdownMenu onClickActionItemCallback: null } - @options = $.extend defaults, options + @options = $.extend defaults, @options @isOpen = false @$menuButton = @$element.find '.dropdown_menu_button'