lib/active_admin/resource.rb in activeadmin-0.1.1 vs lib/active_admin/resource.rb in activeadmin-0.2.0

- old
+ new

@@ -1,19 +1,63 @@ module ActiveAdmin + + # Resource is the primary data storage for resource configuration in Active Admin + # + # When you register a resource (ActiveAdmin.register Post) you are actually creating + # a new Resource instance within the given Namespace. + # + # The instance of the current resource is available in ResourceController and views + # by calling the #active_admin_config method. + # class Resource - attr_reader :namespace, :resource, :page_configs, :member_actions, :collection_actions, - :parent_menu_item_name - attr_accessor :resource_name, :sort_order, :scope_to, :scope_to_association_method + # 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 + 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 + + # 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 + + # The titleized name to use for this resource + attr_accessor :resource_name + + # The default sort order to use in the controller + attr_accessor :sort_order + + # Scope this resource to an association in the controller + 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 + + def initialize(namespace, resource, options = {}) @namespace = namespace @resource = resource @options = default_options.merge(options) @sort_order = @options[:sort_order] @page_configs = {} + @menu_options = {} @member_actions, @collection_actions = [], [] + @scopes = [] end # An underscored safe representation internally for this resource def underscored_resource_name @underscored_resource_name ||= if @options[:as] @@ -49,39 +93,96 @@ @controller ||= controller_name.constantize end # Returns the routes prefix for this resource def route_prefix - controller.resources_configuration[:self][: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_prefix, controller.resources_configuration[:self][:route_collection_name], 'path'].compact.join('_').to_sym end - # Set the menu options + # 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 + + # Set the menu options. To not add this resource to the menu, just + # call #menu(false) def menu(options = {}) - @parent_menu_item_name = options[:parent] + options = options == false ? { :display => false } : options + @menu_options = options end + # Returns the name to put this resource under in the menu + def parent_menu_item_name + @menu_options[:parent] + end + + # Should this resource be added to the menu system? + def include_in_menu? + return false if @menu_options[:display] == false + !(belongs_to? && !belongs_to_config.optional?) + end + # Returns the name to be displayed in the menu for this resource def menu_item_name @menu_item_name ||= plural_resource_name end + # Clears all the member actions this resource knows about def clear_member_actions! @member_actions = [] end def clear_collection_actions! @collection_actions = [] end - # Returns the name of the controller class for this resource - def dashboard_controller_name - [namespace.module_name, "DashboardController"].compact.join("::") + # Return an array of scopes for this resource + def scopes + @scopes + end + + # Returns a scope for this object by its identifier + def get_scope_by_id(id) + id = id.to_s + @scopes.find{|s| s.id == id } + end + + def default_scope + @default_scope + end + + # Create a new scope object for this resource + def scope(*args, &block) + options = args.extract_options! + @scopes << ActiveAdmin::Scope.new(*args, &block) + if options[:default] + @default_scope = @scopes.last + end + end + + # Are admin notes turned on for this resource + def admin_notes? + admin_notes.nil? ? ActiveAdmin.admin_notes : admin_notes + end + + def belongs_to(target, options = {}) + @belongs_to = Resource::BelongsTo.new(self, target, options) + controller.belongs_to(target, options.dup) + end + + def belongs_to_config + @belongs_to + end + + # Do we belong to another resource + def belongs_to? + !belongs_to_config.nil? end private def default_options