lib/active_admin/resource.rb in activeadmin-0.3.4 vs lib/active_admin/resource.rb in activeadmin-0.4.0

- old
+ new

@@ -1,10 +1,13 @@ require 'active_admin/resource/action_items' +require 'active_admin/resource/controllers' require 'active_admin/resource/menu' +require 'active_admin/resource/page_presenters' require 'active_admin/resource/naming' require 'active_admin/resource/scopes' require 'active_admin/resource/sidebars' +require 'active_admin/resource/belongs_to' module ActiveAdmin # Resource is the primary data storage for resource configuration in Active Admin # @@ -17,22 +20,16 @@ class Resource # Event dispatched when a new resource is registered RegisterEvent = 'active_admin.resource.register'.freeze - autoload :BelongsTo, 'active_admin/resource/belongs_to' - - # The namespace this resource belongs to + # The namespace this config belongs to attr_reader :namespace - # The class this resource wraps. If you register the Post model, Resource#resource - # will point to the Post class - attr_reader :resource + # The name of the resource class + attr_reader :resource_class_name - # A hash of page configurations for the controller indexed by action name - attr_reader :page_configs - # An array of member actions defined for this resource attr_reader :member_actions # An array of collection actions defined for this resource attr_reader :collection_actions @@ -44,74 +41,61 @@ attr_accessor :scope_to # If we're scoping resources, use this method on the parent to return the collection attr_accessor :scope_to_association_method - # Set to false to turn off admin notes - attr_accessor :admin_notes - # Set the configuration for the CSV attr_writer :csv_builder module Base - def initialize(namespace, resource, options = {}) + def initialize(namespace, resource_class, options = {}) @namespace = namespace - @resource = resource + @resource_class_name = "::#{resource_class.name}" @options = default_options.merge(options) @sort_order = @options[:sort_order] - @page_configs = {} @member_actions, @collection_actions = [], [] end end include Base + include Controllers + include PagePresenters include ActionItems - include Menu include Naming include Scopes include Sidebars + include Menu + # The class this resource wraps. If you register the Post model, Resource#resource_class + # will point to the Post class + def resource_class + ActiveSupport::Dependencies.constantize(resource_class_name) + end def resource_table_name - resource.quoted_table_name + resource_class.quoted_table_name end - # Returns a properly formatted controller name for this - # resource within its namespace - def controller_name - [namespace.module_name, camelized_resource_name.pluralize + "Controller"].compact.join('::') + # Returns the named route for an instance of this resource + def route_instance_path + [route_prefix, controller.resources_configuration[:self][:route_instance_name], 'path'].compact.join('_').to_sym end - # Returns the controller for this resource - def controller - @controller ||= controller_name.constantize - end - - # Returns the routes prefix for this resource - def route_prefix - namespace.module_name.try(:underscore) - end - # Returns a symbol for the route to use to get to the # collection of this resource def route_collection_path - route = [route_prefix, controller.resources_configuration[:self][:route_collection_name]] + route = super + # Handle plural resources. if controller.resources_configuration[:self][:route_collection_name] == - controller.resources_configuration[:self][:route_instance_name] - route << "index" + controller.resources_configuration[:self][:route_instance_name] + route = route.to_s.gsub('_path', '_index_path').to_sym end - route << 'path' - route.compact.join('_').to_sym + route end - # Returns the named route for an instance of this resource - def route_instance_path - [route_prefix, controller.resources_configuration[:self][:route_instance_name], 'path'].compact.join('_').to_sym - end - # Clears all the member actions this resource knows about def clear_member_actions! @member_actions = [] end @@ -136,24 +120,34 @@ # Do we belong to another resource def belongs_to? !belongs_to_config.nil? end + def include_in_menu? + super && !(belongs_to? && !belongs_to_config.optional?) + end + # The csv builder for this resource def csv_builder @csv_builder || default_csv_builder end + # @deprecated + def resource + resource_class + end + ActiveAdmin::Deprecation.deprecate self, :resource, + "ActiveAdmin::Resource#resource is deprecated. Please use #resource_class instead." + private def default_options { - :namespace => ActiveAdmin.application.default_namespace, - :sort_order => "#{resource.respond_to?(:primary_key) ? resource.primary_key : 'id'}_desc" + :sort_order => "#{resource_class.respond_to?(:primary_key) ? resource_class.primary_key : 'id'}_desc" } end def default_csv_builder - @default_csv_builder ||= CSVBuilder.default_for_resource(resource) + @default_csv_builder ||= CSVBuilder.default_for_resource(resource_class) end end # class Resource end # module ActiveAdmin