activeadmin 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activeadmin might be problematic. Click here for more details.
- data/.gitignore +1 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +164 -5
- data/CONTRIBUTING.md +113 -0
- data/Gemfile +7 -9
- data/LICENSE +1 -1
- data/README.rdoc +19 -20
- data/activeadmin.gemspec +4 -2
- data/app/assets/javascripts/active_admin/application.js +9 -0
- data/app/assets/javascripts/active_admin/base.js +4 -12
- data/app/assets/stylesheets/active_admin/_base.css.scss +17 -341
- data/app/assets/stylesheets/active_admin/_forms.css.scss +13 -7
- data/app/assets/stylesheets/active_admin/_header.css.scss +23 -4
- data/app/assets/stylesheets/active_admin/components/_blank_slates.scss +31 -0
- data/app/assets/stylesheets/active_admin/components/_breadcrumbs.scss +20 -0
- data/app/assets/stylesheets/active_admin/components/_buttons.scss +12 -0
- data/app/assets/stylesheets/active_admin/components/_flash_messages.css.scss +2 -0
- data/app/assets/stylesheets/active_admin/components/_grid.scss +9 -0
- data/app/assets/stylesheets/active_admin/components/_links.scss +5 -0
- data/app/assets/stylesheets/active_admin/components/_pagination.scss +34 -0
- data/app/assets/stylesheets/active_admin/components/_panels.scss +6 -0
- data/app/assets/stylesheets/active_admin/components/_scopes.scss +10 -0
- data/app/assets/stylesheets/active_admin/components/_status_tags.scss +12 -0
- data/app/assets/stylesheets/active_admin/components/_table_tools.css.scss +101 -0
- data/app/assets/stylesheets/active_admin/components/_tables.css.scss +43 -2
- data/app/assets/stylesheets/active_admin/mixins/_all.css.scss +2 -1
- data/app/assets/stylesheets/active_admin/mixins/_buttons.css.scss +50 -13
- data/app/assets/stylesheets/active_admin/mixins/_gradients.css.scss +9 -1
- data/app/assets/stylesheets/active_admin/mixins/_sections.css.scss +7 -5
- data/app/assets/stylesheets/active_admin/mixins/_shadows.css.scss +1 -0
- data/app/assets/stylesheets/active_admin/mixins/_typography.scss +3 -0
- data/app/assets/stylesheets/active_admin/mixins/_utilities.scss +9 -0
- data/app/assets/stylesheets/active_admin/mixins/_variables.css.scss +1 -0
- data/app/assets/stylesheets/active_admin/pages/_dashboard.scss +5 -0
- data/app/assets/stylesheets/active_admin/pages/_logged_out.scss +44 -0
- data/app/assets/stylesheets/active_admin/structure/_footer.scss +14 -0
- data/app/assets/stylesheets/active_admin/structure/_main_structure.scss +26 -0
- data/app/assets/stylesheets/active_admin/structure/_title_bar.scss +44 -0
- data/app/views/active_admin/devise/passwords/edit.html.erb +1 -1
- data/app/views/active_admin/devise/passwords/new.html.erb +1 -1
- data/app/views/active_admin/devise/sessions/new.html.erb +1 -1
- data/app/views/active_admin/devise/shared/_links.erb +1 -1
- data/app/views/active_admin/devise/unlocks/new.html.erb +1 -1
- data/app/views/active_admin/page/index.html.arb +1 -0
- data/app/views/layouts/active_admin_logged_out.html.erb +2 -2
- data/cucumber.yml +3 -2
- data/docs/1-general-configuration.md +46 -10
- data/docs/6-show-screens.md +7 -0
- data/docs/8-custom-actions.md +12 -2
- data/docs/9-custom-pages.md +84 -0
- data/features/belongs_to.feature +27 -0
- data/features/comments/commenting.feature +3 -2
- data/features/dashboard.feature +18 -0
- data/features/development_reloading.feature +43 -0
- data/features/index/filter_with_check_boxes.feature +25 -0
- data/features/index/index_as_table.feature +14 -0
- data/features/index/index_scopes.feature +59 -0
- data/features/index/pagination.feature +19 -1
- data/features/menu.feature +22 -2
- data/features/registering_assets.feature +2 -2
- data/features/registering_pages.feature +66 -0
- data/features/specifying_actions.feature +5 -0
- data/features/step_definitions/action_item_steps.rb +2 -6
- data/features/step_definitions/action_link_steps.rb +7 -0
- data/features/step_definitions/additional_web_steps.rb +11 -3
- data/features/step_definitions/asset_steps.rb +5 -1
- data/features/step_definitions/breadcrumb_steps.rb +5 -0
- data/features/step_definitions/comment_steps.rb +3 -3
- data/features/step_definitions/configuration_steps.rb +69 -13
- data/features/step_definitions/dashboard_steps.rb +7 -3
- data/features/step_definitions/factory_steps.rb +9 -2
- data/features/step_definitions/flash_steps.rb +9 -1
- data/features/step_definitions/format_steps.rb +9 -1
- data/features/step_definitions/index_scope_steps.rb +18 -6
- data/features/step_definitions/member_link_steps.rb +7 -0
- data/features/step_definitions/pagination_steps.rb +2 -2
- data/features/step_definitions/site_title_steps.rb +15 -0
- data/features/step_definitions/tab_steps.rb +1 -1
- data/features/step_definitions/user_steps.rb +1 -1
- data/features/step_definitions/web_steps.rb +4 -4
- data/features/support/paths.rb +6 -0
- data/features/users/logging_out.feature +11 -0
- data/lib/active_admin.rb +26 -10
- data/lib/active_admin/application.rb +64 -22
- data/lib/active_admin/arbre.rb +0 -1
- data/lib/active_admin/arbre/builder.rb +8 -5
- data/lib/active_admin/arbre/context.rb +1 -1
- data/lib/active_admin/arbre/html/attributes.rb +1 -1
- data/lib/active_admin/arbre/html/class_list.rb +0 -4
- data/lib/active_admin/arbre/html/collection.rb +2 -2
- data/lib/active_admin/arbre/html/document.rb +1 -1
- data/lib/active_admin/arbre/html/element.rb +19 -9
- data/lib/active_admin/arbre/html/tag.rb +3 -3
- data/lib/active_admin/arbre/html/text_node.rb +2 -2
- data/lib/active_admin/asset_registration.rb +15 -2
- data/lib/active_admin/base_controller.rb +61 -0
- data/lib/active_admin/{resource_controller → base_controller}/menu.rb +1 -1
- data/lib/active_admin/callbacks.rb +16 -18
- data/lib/active_admin/comments.rb +48 -54
- data/lib/active_admin/comments/comment.rb +13 -2
- data/lib/active_admin/comments/namespace_helper.rb +1 -1
- data/lib/active_admin/comments/views/active_admin_comments.rb +7 -3
- data/lib/active_admin/dashboards.rb +4 -0
- data/lib/active_admin/dashboards/dashboard_controller.rb +21 -5
- data/lib/active_admin/dsl.rb +6 -159
- data/lib/active_admin/event.rb +5 -3
- data/lib/active_admin/filter_form_builder.rb +53 -0
- data/lib/active_admin/form_builder.rb +25 -19
- data/lib/active_admin/helpers/settings.rb +6 -10
- data/lib/active_admin/inputs.rb +14 -0
- data/lib/active_admin/inputs/datepicker_input.rb +11 -0
- data/lib/active_admin/inputs/filter_base.rb +46 -0
- data/lib/active_admin/inputs/filter_check_boxes_input.rb +40 -0
- data/lib/active_admin/inputs/filter_date_range_input.rb +34 -0
- data/lib/active_admin/inputs/filter_numeric_input.rb +55 -0
- data/lib/active_admin/inputs/filter_select_input.rb +23 -0
- data/lib/active_admin/inputs/filter_string_input.rb +22 -0
- data/lib/active_admin/locales/ca.yml +44 -0
- data/lib/active_admin/locales/cs.yml +14 -10
- data/lib/active_admin/locales/de.yml +44 -0
- data/lib/active_admin/locales/en.yml +5 -0
- data/lib/active_admin/locales/hr.yml +40 -0
- data/lib/active_admin/locales/hu.yml +45 -0
- data/lib/active_admin/locales/it.yml +11 -6
- data/lib/active_admin/locales/ko.yml +40 -0
- data/lib/active_admin/locales/lv.yml +43 -0
- data/lib/active_admin/locales/nl.yml +40 -0
- data/lib/active_admin/locales/no-NB.yml +40 -0
- data/lib/active_admin/locales/pl.yml +7 -1
- data/lib/active_admin/locales/pt-BR.yml +6 -2
- data/lib/active_admin/menu_item.rb +25 -17
- data/lib/active_admin/namespace.rb +48 -33
- data/lib/active_admin/page.rb +65 -0
- data/lib/active_admin/page_controller.rb +15 -0
- data/lib/active_admin/page_dsl.rb +21 -0
- data/lib/active_admin/page_presenter.rb +30 -0
- data/lib/active_admin/reloader.rb +109 -42
- data/lib/active_admin/resource.rb +38 -44
- data/lib/active_admin/resource/action_items.rb +5 -0
- data/lib/active_admin/resource/belongs_to.rb +4 -2
- data/lib/active_admin/resource/controllers.rb +35 -0
- data/lib/active_admin/resource/menu.rb +1 -2
- data/lib/active_admin/resource/naming.rb +53 -31
- data/lib/active_admin/resource/page_presenters.rb +28 -0
- data/lib/active_admin/resource/scopes.rb +16 -3
- data/lib/active_admin/resource/sidebars.rb +4 -0
- data/lib/active_admin/resource_collection.rb +88 -0
- data/lib/active_admin/resource_controller.rb +18 -54
- data/lib/active_admin/resource_controller/action_builder.rb +1 -1
- data/lib/active_admin/resource_controller/actions.rb +8 -8
- data/lib/active_admin/resource_controller/callbacks.rb +1 -1
- data/lib/active_admin/resource_controller/collection.rb +8 -4
- data/lib/active_admin/resource_controller/filters.rb +1 -1
- data/lib/active_admin/resource_controller/resource_class_methods.rb +24 -0
- data/lib/active_admin/resource_controller/scoping.rb +1 -1
- data/lib/active_admin/resource_controller/sidebars.rb +1 -1
- data/lib/active_admin/resource_dsl.rb +157 -0
- data/lib/active_admin/router.rb +21 -14
- data/lib/active_admin/scope.rb +15 -3
- data/lib/active_admin/version.rb +1 -1
- data/lib/active_admin/view_factory.rb +4 -3
- data/lib/active_admin/view_helpers/auto_link_helper.rb +1 -10
- data/lib/active_admin/view_helpers/breadcrumb_helper.rb +25 -21
- data/lib/active_admin/view_helpers/filter_form_helper.rb +0 -150
- data/lib/active_admin/views/components/attributes_table.rb +1 -1
- data/lib/active_admin/views/components/columns.rb +4 -4
- data/lib/active_admin/views/components/paginated_collection.rb +42 -13
- data/lib/active_admin/views/components/panel.rb +8 -0
- data/lib/active_admin/views/components/scopes.rb +24 -18
- data/lib/active_admin/views/components/status_tag.rb +6 -5
- data/lib/active_admin/views/components/table_for.rb +6 -2
- data/lib/active_admin/views/header_renderer.rb +31 -12
- data/lib/active_admin/views/index_as_block.rb +2 -2
- data/lib/active_admin/views/index_as_blog.rb +3 -3
- data/lib/active_admin/views/index_as_grid.rb +4 -4
- data/lib/active_admin/views/index_as_table.rb +13 -6
- data/lib/active_admin/views/pages/base.rb +4 -4
- data/lib/active_admin/views/pages/form.rb +49 -0
- data/lib/active_admin/views/pages/index.rb +18 -6
- data/lib/active_admin/views/pages/page.rb +24 -0
- data/lib/active_admin/views/pages/show.rb +1 -1
- data/lib/generators/active_admin/assets/assets_generator.rb +19 -1
- data/lib/generators/active_admin/install/templates/active_admin.rb.erb +38 -12
- data/lib/generators/active_admin/install/templates/dashboards.rb +6 -0
- data/script/use_rails +14 -7
- data/spec/spec_helper.rb +9 -14
- data/spec/spec_helper_without_rails.rb +10 -0
- data/spec/support/detect_rails_version.rb +22 -3
- data/spec/support/integration_example_group.rb +5 -7
- data/spec/support/rails_template.rb +11 -0
- data/spec/support/rails_template_with_data.rb +25 -0
- data/spec/support/templates/cucumber_with_reloading.rb +5 -0
- data/spec/unit/active_admin_spec.rb +8 -0
- data/spec/unit/application_spec.rb +48 -2
- data/spec/unit/arbre/context_spec.rb +1 -1
- data/spec/unit/arbre/html/element_finder_methods_spec.rb +58 -2
- data/spec/unit/arbre/html/element_spec.rb +18 -5
- data/spec/unit/arbre/html/tag_attributes_spec.rb +3 -3
- data/spec/unit/arbre/html/tag_spec.rb +1 -1
- data/spec/unit/arbre/html_spec.rb +32 -22
- data/spec/unit/asset_registration_spec.rb +9 -3
- data/spec/unit/auto_link_spec.rb +2 -2
- data/spec/unit/base_controller_shared_examples.rb +28 -0
- data/spec/unit/base_controller_spec.rb +8 -0
- data/spec/unit/belongs_to_spec.rb +30 -33
- data/spec/unit/comments_spec.rb +45 -15
- data/spec/unit/config_shared_examples.rb +119 -0
- data/spec/unit/dashboard_controller_spec.rb +44 -0
- data/spec/unit/event_spec.rb +6 -0
- data/spec/unit/filter_form_builder_spec.rb +9 -0
- data/spec/unit/form_builder_spec.rb +8 -14
- data/spec/unit/menu_item_spec.rb +25 -20
- data/spec/unit/namespace/register_page_spec.rb +102 -0
- data/spec/unit/namespace/register_resource_spec.rb +188 -0
- data/spec/unit/namespace_spec.rb +11 -183
- data/spec/unit/page_controller_spec.rb +8 -0
- data/spec/unit/page_spec.rb +60 -0
- data/spec/unit/reloader_spec.rb +82 -38
- data/spec/unit/resource/menu_spec.rb +1 -51
- data/spec/unit/resource/naming_spec.rb +25 -20
- data/spec/unit/resource/page_presenters_spec.rb +32 -0
- data/spec/unit/resource/scopes_spec.rb +13 -0
- data/spec/unit/resource_collection_spec.rb +101 -0
- data/spec/unit/resource_controller_spec.rb +40 -32
- data/spec/unit/{registration_spec.rb → resource_registration_spec.rb} +0 -0
- data/spec/unit/resource_spec.rb +8 -24
- data/spec/unit/routing_spec.rb +50 -1
- data/spec/unit/scope_spec.rb +18 -4
- data/spec/unit/views/components/paginated_collection_spec.rb +163 -0
- data/spec/unit/views/components/panel_spec.rb +9 -0
- data/spec/unit/views/components/status_tag_spec.rb +9 -0
- data/spec/unit/views/tabbed_navigation_spec.rb +2 -1
- data/tasks/test.rake +44 -26
- metadata +176 -69
- data/app/assets/javascripts/active_admin/vendor.js +0 -382
- data/lib/active_admin/arbre/core_extensions.rb +0 -5
- data/lib/active_admin/comments/configuration.rb +0 -18
- data/lib/active_admin/page_config.rb +0 -15
- data/lib/active_admin/resource_controller/form.rb +0 -42
- data/lib/active_admin/resource_controller/page_configurations.rb +0 -53
- data/lib/active_admin/views/pages/edit.rb +0 -28
- data/lib/active_admin/views/pages/new.rb +0 -28
- data/lib/generators/active_admin/assets/templates/3.0/active_admin.js +0 -427
- data/spec/integration/belongs_to_spec.rb +0 -42
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_admin/resource'
|
2
|
+
|
1
3
|
module ActiveAdmin
|
2
4
|
class Resource
|
3
5
|
class BelongsTo
|
@@ -14,8 +16,8 @@ module ActiveAdmin
|
|
14
16
|
|
15
17
|
# Returns the target resource class or raises an exception if it doesn't exist
|
16
18
|
def target
|
17
|
-
namespace.resources
|
18
|
-
raise TargetNotFound, "Could not find registered resource #{@target_name} in #{namespace.name} with #{namespace.resources.
|
19
|
+
namespace.resources.find_by_key(@target_name.to_s.camelize) or
|
20
|
+
raise TargetNotFound, "Could not find registered resource #{@target_name} in #{namespace.name} with #{namespace.resources.inspect}"
|
19
21
|
end
|
20
22
|
|
21
23
|
def namespace
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ActiveAdmin
|
2
|
+
class Resource
|
3
|
+
module Controllers
|
4
|
+
|
5
|
+
# Returns a properly formatted controller name for this
|
6
|
+
# config within its namespace
|
7
|
+
def controller_name
|
8
|
+
[namespace.module_name, plural_camelized_resource_name + "Controller"].compact.join('::')
|
9
|
+
end
|
10
|
+
|
11
|
+
# Returns the controller for this config
|
12
|
+
def controller
|
13
|
+
@controller ||= controller_name.constantize
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the routes prefix for this config
|
17
|
+
def route_prefix
|
18
|
+
namespace.module_name.try(:underscore)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns a symbol for the route to use to get to the
|
22
|
+
# collection of this resource
|
23
|
+
def route_collection_path
|
24
|
+
route = [
|
25
|
+
route_prefix,
|
26
|
+
controller.resources_configuration[:self][:route_collection_name],
|
27
|
+
'path'
|
28
|
+
]
|
29
|
+
|
30
|
+
route.compact.join('_').to_sym
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,49 +1,71 @@
|
|
1
1
|
module ActiveAdmin
|
2
2
|
class Resource
|
3
3
|
module Naming
|
4
|
+
# Returns the user facing name. Example: "Bank Account"
|
5
|
+
def resource_name
|
6
|
+
@resource_name ||= @options[:as]
|
7
|
+
@resource_name ||= singular_human_name
|
8
|
+
@resource_name ||= safe_resource_name
|
9
|
+
end
|
4
10
|
|
5
|
-
#
|
6
|
-
def
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
# Returns the plural version of the user facing name. Example: "Bank Accounts"
|
12
|
+
def plural_resource_name
|
13
|
+
@plural_resource_name ||= @options[:as].pluralize if @options[:as]
|
14
|
+
@plural_resource_name ||= plural_human_name
|
15
|
+
@plural_resource_name ||= resource_name.pluralize
|
16
|
+
end
|
17
|
+
|
18
|
+
# A name used internally to uniquely identify this resource
|
19
|
+
def resource_key
|
20
|
+
camelized_resource_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def safe_resource_name
|
24
|
+
@safe_resource_name ||= @options[:as]
|
25
|
+
@safe_resource_name ||= resource_class.name.gsub('::',' ')
|
26
|
+
end
|
27
|
+
|
28
|
+
def plural_safe_resource_name
|
29
|
+
safe_resource_name.pluralize
|
12
30
|
end
|
13
31
|
|
14
32
|
# A camelized safe representation for this resource
|
15
33
|
def camelized_resource_name
|
16
|
-
|
34
|
+
safe_resource_name.titleize.gsub(' ', '')
|
17
35
|
end
|
18
36
|
|
19
|
-
|
20
|
-
|
21
|
-
def resource_name
|
22
|
-
@resource_name ||= if @options[:as] || !resource.respond_to?(:model_name)
|
23
|
-
underscored_resource_name.titleize
|
24
|
-
else
|
25
|
-
resource.model_name.human.titleize
|
26
|
-
end
|
37
|
+
def plural_camelized_resource_name
|
38
|
+
plural_safe_resource_name.titleize.gsub(' ', '')
|
27
39
|
end
|
28
40
|
|
29
|
-
#
|
30
|
-
def
|
31
|
-
|
32
|
-
resource_name.pluralize
|
33
|
-
else
|
34
|
-
# Check if we have a translation available otherwise pluralize
|
35
|
-
begin
|
36
|
-
I18n.translate!("activerecord.models.#{resource.model_name.underscore}")
|
37
|
-
resource.model_name.human(:count => 3)
|
38
|
-
rescue I18n::MissingTranslationData
|
39
|
-
resource_name.pluralize
|
40
|
-
end
|
41
|
-
end
|
41
|
+
# An underscored safe representation internally for this resource
|
42
|
+
def underscored_resource_name
|
43
|
+
camelized_resource_name.underscore
|
42
44
|
end
|
43
|
-
|
45
|
+
|
44
46
|
# Returns the plural and underscored version of this resource. Useful for element id's.
|
45
47
|
def plural_underscored_resource_name
|
46
|
-
|
48
|
+
plural_camelized_resource_name.underscore
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
# @return [String] Titleized human name via ActiveRecord I18n or nil
|
55
|
+
def singular_human_name
|
56
|
+
return nil unless resource_class.respond_to?(:model_name)
|
57
|
+
resource_class.model_name.human.titleize
|
58
|
+
end
|
59
|
+
|
60
|
+
# @return [String] Titleized plural human name via ActiveRecord I18n or nil
|
61
|
+
def plural_human_name
|
62
|
+
return nil unless resource_class.respond_to?(:model_name)
|
63
|
+
|
64
|
+
begin
|
65
|
+
I18n.translate!("activerecord.models.#{resource_class.model_name.underscore}.other").titleize
|
66
|
+
rescue I18n::MissingTranslationData
|
67
|
+
nil
|
68
|
+
end
|
47
69
|
end
|
48
70
|
end
|
49
71
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ActiveAdmin
|
2
|
+
class Resource
|
3
|
+
module PagePresenters
|
4
|
+
|
5
|
+
# A hash of page configurations for the controller indexed by action name
|
6
|
+
def page_presenters
|
7
|
+
@page_presenters ||= {}
|
8
|
+
end
|
9
|
+
|
10
|
+
# Sets a page config for a given action
|
11
|
+
#
|
12
|
+
# @param [String, Symbol] action The action to store this configuration for
|
13
|
+
# @param [PagePresenter] page_presenter The instance of PagePresenter to store
|
14
|
+
def set_page_presenter(action, page_presenter)
|
15
|
+
page_presenters[action.to_sym] = page_presenter
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns a stored page config
|
19
|
+
#
|
20
|
+
# @param [Symbol, String] action The action to get the config for
|
21
|
+
# @returns [PagePresenter, nil]
|
22
|
+
def get_page_presenter(action)
|
23
|
+
page_presenters[action.to_sym]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -22,10 +22,23 @@ module ActiveAdmin
|
|
22
22
|
# to your i18n files a key like "active_admin.scopes.scope_method".
|
23
23
|
def scope(*args, &block)
|
24
24
|
options = args.extract_options!
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
title = args[0] rescue nil
|
26
|
+
method = args[1] rescue nil
|
27
|
+
|
28
|
+
scope = ActiveAdmin::Scope.new(title, method, options, &block)
|
29
|
+
|
30
|
+
# Finds and replaces a scope by the same name if it already exists
|
31
|
+
existing_scope_index = scopes.index{|existing_scope| existing_scope.id == scope.id }
|
32
|
+
if existing_scope_index
|
33
|
+
scopes.delete_at(existing_scope_index)
|
34
|
+
scopes.insert(existing_scope_index, scope)
|
35
|
+
else
|
36
|
+
self.scopes << scope
|
28
37
|
end
|
38
|
+
|
39
|
+
@default_scope = scope if options[:default]
|
40
|
+
|
41
|
+
scope
|
29
42
|
end
|
30
43
|
|
31
44
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module ActiveAdmin
|
2
|
+
|
3
|
+
class ResourceMismatchError < StandardError; end
|
4
|
+
|
5
|
+
# Holds on to a collection of Resources. Is an Enumerable object
|
6
|
+
# so it has some Array like qualities.
|
7
|
+
#
|
8
|
+
# Adding a resource assumes that the object responds to #resource_key
|
9
|
+
class ResourceCollection
|
10
|
+
include Enumerable
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@resource_hash = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Add a new resource to the collection. If the resource_key already
|
17
|
+
# exists, the exiting resource is returned.
|
18
|
+
#
|
19
|
+
# @param [Resource, Page] resource The resource to add to the collection
|
20
|
+
#
|
21
|
+
# @returns [Resource, Page] Either the existing resource or the new one
|
22
|
+
def add(resource)
|
23
|
+
if has_key?(resource.resource_key)
|
24
|
+
existing_resource = find_by_key(resource.resource_key)
|
25
|
+
ensure_resource_classes_match!(existing_resource, resource)
|
26
|
+
existing_resource
|
27
|
+
else
|
28
|
+
@resource_hash[resource.resource_key] = resource
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# @returns [Array] An array of all the resources
|
33
|
+
def resources
|
34
|
+
@resource_hash.values
|
35
|
+
end
|
36
|
+
|
37
|
+
# For enumerable
|
38
|
+
def each(&block)
|
39
|
+
@resource_hash.values.each(&block)
|
40
|
+
end
|
41
|
+
|
42
|
+
# @returns [Array] An array of all the keys registered in the collection
|
43
|
+
def keys
|
44
|
+
@resource_hash.keys
|
45
|
+
end
|
46
|
+
|
47
|
+
# @returns [Boolean] If the key has been registered in the collection
|
48
|
+
def has_key?(resource_key)
|
49
|
+
@resource_hash.has_key?(resource_key)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Finds a resource by a given key
|
53
|
+
def find_by_key(resource_key)
|
54
|
+
@resource_hash[resource_key]
|
55
|
+
end
|
56
|
+
|
57
|
+
# Finds a resource based on it's class. Looks up the class Heirarchy if its
|
58
|
+
# a subclass of an Active Record class (ie: implementes base_class)
|
59
|
+
def find_by_resource_class(resource_class)
|
60
|
+
resource_class_name = resource_class.to_s
|
61
|
+
match = resources_with_a_resource_class.find{|r| r.resource_class.to_s == resource_class_name }
|
62
|
+
return match if match
|
63
|
+
|
64
|
+
if resource_class.respond_to?(:base_class)
|
65
|
+
base_class_name = resource_class.base_class.to_s
|
66
|
+
resources_with_a_resource_class.find{|r| r.resource_class.to_s == base_class_name }
|
67
|
+
else
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def resources_with_a_resource_class
|
75
|
+
select{|resource| resource.respond_to?(:resource_class) }
|
76
|
+
end
|
77
|
+
|
78
|
+
def ensure_resource_classes_match!(existing_resource, resource)
|
79
|
+
return unless existing_resource.respond_to?(:resource_class) && resource.respond_to?(:resource_class)
|
80
|
+
|
81
|
+
if existing_resource.resource_class != resource.resource_class
|
82
|
+
raise ActiveAdmin::ResourceMismatchError,
|
83
|
+
"Tried to register #{resource.resource_class} as #{resource.resource_key} but already registered to #{existing_resource.resource_class}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
@@ -4,24 +4,18 @@ require 'active_admin/resource_controller/action_builder'
|
|
4
4
|
require 'active_admin/resource_controller/callbacks'
|
5
5
|
require 'active_admin/resource_controller/collection'
|
6
6
|
require 'active_admin/resource_controller/filters'
|
7
|
-
require 'active_admin/resource_controller/form'
|
8
|
-
require 'active_admin/resource_controller/menu'
|
9
|
-
require 'active_admin/resource_controller/page_configurations'
|
10
7
|
require 'active_admin/resource_controller/scoping'
|
8
|
+
require 'active_admin/resource_controller/resource_class_methods'
|
11
9
|
|
12
10
|
module ActiveAdmin
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
# All Resources Controller inherits from this controller.
|
12
|
+
# It implements actions and helpers for resources.
|
13
|
+
class ResourceController < BaseController
|
17
14
|
layout :determine_active_admin_layout
|
18
15
|
|
19
16
|
respond_to :html, :xml, :json
|
20
17
|
respond_to :csv, :only => :index
|
21
18
|
|
22
|
-
before_filter :only_render_implemented_actions
|
23
|
-
before_filter :authenticate_active_admin_user
|
24
|
-
|
25
19
|
ACTIVE_ADMIN_ACTIONS = [:index, :show, :new, :create, :edit, :update, :destroy]
|
26
20
|
|
27
21
|
include Actions
|
@@ -29,37 +23,30 @@ module ActiveAdmin
|
|
29
23
|
include Callbacks
|
30
24
|
include Collection
|
31
25
|
include Filters
|
32
|
-
include Form
|
33
|
-
include Menu
|
34
|
-
include PageConfigurations
|
35
26
|
include Scoping
|
27
|
+
extend ResourceClassMethods
|
36
28
|
|
37
29
|
class << self
|
38
|
-
# Ensure that this method is available for the DSL
|
39
|
-
public :actions
|
40
|
-
|
41
|
-
# Reference to the Resource object which initialized
|
42
|
-
# this controller
|
43
|
-
attr_accessor :active_admin_config
|
44
|
-
|
45
30
|
def active_admin_config=(config)
|
46
31
|
@active_admin_config = config
|
47
|
-
|
32
|
+
|
33
|
+
defaults :resource_class => config.resource_class,
|
48
34
|
:route_prefix => config.route_prefix,
|
49
35
|
:instance_name => config.underscored_resource_name
|
50
36
|
end
|
51
37
|
|
38
|
+
# Inherited Resources uses the inherited(base) hook method to
|
39
|
+
# add in the Base.resource_class class method. To override it, we
|
40
|
+
# need to install our resource_class method each time we're inherited from.
|
41
|
+
def inherited(base)
|
42
|
+
super(base)
|
43
|
+
base.override_resource_class_methods!
|
44
|
+
end
|
45
|
+
|
52
46
|
public :belongs_to
|
53
47
|
end
|
54
48
|
|
55
|
-
|
56
|
-
|
57
|
-
# By default Rails will render un-implemented actions when the view exists. Becuase Active
|
58
|
-
# Admin allows you to not render any of the actions by using the #actions method, we need
|
59
|
-
# to check if they are implemented.
|
60
|
-
def only_render_implemented_actions
|
61
|
-
raise AbstractController::ActionNotFound unless action_methods.include?(params[:action])
|
62
|
-
end
|
49
|
+
private
|
63
50
|
|
64
51
|
# Determine which layout to use.
|
65
52
|
#
|
@@ -72,34 +59,11 @@ module ActiveAdmin
|
|
72
59
|
ACTIVE_ADMIN_ACTIONS.include?(params[:action].to_sym) ? false : 'active_admin'
|
73
60
|
end
|
74
61
|
|
75
|
-
# Calls the authentication method as defined in ActiveAdmin.authentication_method
|
76
|
-
def authenticate_active_admin_user
|
77
|
-
send(active_admin_application.authentication_method) if active_admin_application.authentication_method
|
78
|
-
end
|
79
|
-
|
80
|
-
def current_active_admin_user
|
81
|
-
send(active_admin_application.current_user_method) if active_admin_application.current_user_method
|
82
|
-
end
|
83
|
-
helper_method :current_active_admin_user
|
84
|
-
|
85
|
-
def current_active_admin_user?
|
86
|
-
!current_active_admin_user.nil?
|
87
|
-
end
|
88
|
-
helper_method :current_active_admin_user?
|
89
|
-
|
90
|
-
def active_admin_config
|
91
|
-
self.class.active_admin_config
|
92
|
-
end
|
93
|
-
helper_method :active_admin_config
|
94
|
-
|
95
|
-
def active_admin_application
|
96
|
-
ActiveAdmin.application
|
97
|
-
end
|
98
|
-
|
99
62
|
# Returns the renderer class to use for the given action.
|
100
63
|
def renderer_for(action)
|
101
|
-
|
64
|
+
active_admin_namespace.view_factory["#{action}_page"]
|
102
65
|
end
|
103
66
|
helper_method :renderer_for
|
67
|
+
|
104
68
|
end
|
105
69
|
end
|