lib/active_admin/resource.rb in activeadmin-0.2.2 vs lib/active_admin/resource.rb in activeadmin-0.3.0

- old
+ new

@@ -1,5 +1,11 @@ +require 'active_admin/resource/action_items' +require 'active_admin/resource/menu' +require 'active_admin/resource/naming' +require 'active_admin/resource/scopes' +require 'active_admin/resource/sidebars' + 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 @@ -29,13 +35,10 @@ 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 @@ -44,48 +47,34 @@ 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 - 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] - @options[:as].gsub(' ', '').underscore.singularize - else - resource.name.gsub('::','').underscore + module Base + def initialize(namespace, resource, options = {}) + @namespace = namespace + @resource = resource + @options = default_options.merge(options) + @sort_order = @options[:sort_order] + @page_configs = {} + @member_actions, @collection_actions = [], [] end end - # A camelized safe representation for this resource - def camelized_resource_name - underscored_resource_name.camelize - end + include Base + include ActionItems + include Menu + include Naming + include Scopes + include Sidebars - # Returns the name to call this resource - def resource_name - @resource_name ||= underscored_resource_name.titleize - end - # Returns the plural version of this resource - def plural_resource_name - @plural_resource_name ||= resource_name.pluralize - end - def resource_table_name - resource.table_name + resource.quoted_table_name end # Returns a properly formatted controller name for this # resource within its namespace def controller_name @@ -103,75 +92,35 @@ 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 + route = [route_prefix, controller.resources_configuration[:self][:route_collection_name]] + + if controller.resources_configuration[:self][:route_collection_name] == + controller.resources_configuration[:self][:route_instance_name] + route << "index" + end + + route << 'path' + route.compact.join('_').to_sym 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 - # Set the menu options. To not add this resource to the menu, just - # call #menu(false) - def menu(options = {}) - 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 - - # Returns the name to be displayed in the menu for this resource - def menu_item_name - @menu_options[:label] || plural_resource_name - 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 - - # Clears all the member actions this resource knows about def clear_member_actions! @member_actions = [] end def clear_collection_actions! @collection_actions = [] end - # 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 @@ -187,16 +136,24 @@ # Do we belong to another resource def belongs_to? !belongs_to_config.nil? end + # The csv builder for this resource + def csv_builder + @csv_builder || default_csv_builder + end + private def default_options { - :namespace => ActiveAdmin.default_namespace, - :sort_order => ActiveAdmin.default_sort_order, + :namespace => ActiveAdmin.application.default_namespace, + :sort_order => "#{resource.respond_to?(:primary_key) ? resource.primary_key : 'id'}_desc" } end - end -end + def default_csv_builder + @default_csv_builder ||= CSVBuilder.default_for_resource(resource) + end + end # class Resource +end # module ActiveAdmin