/lib/active_admin/resource.rb

https://github.com/dickeyxxx/active_admin · Ruby · 159 lines · 99 code · 32 blank · 28 comment · 4 complexity · c51a55233774481df508b808c71bf664 MD5 · raw file

  1. require 'active_admin/resource/action_items'
  2. require 'active_admin/resource/controllers'
  3. require 'active_admin/resource/menu'
  4. require 'active_admin/resource/page_presenters'
  5. require 'active_admin/resource/pagination'
  6. require 'active_admin/resource/naming'
  7. require 'active_admin/resource/scopes'
  8. require 'active_admin/resource/sidebars'
  9. require 'active_admin/resource/belongs_to'
  10. module ActiveAdmin
  11. # Resource is the primary data storage for resource configuration in Active Admin
  12. #
  13. # When you register a resource (ActiveAdmin.register Post) you are actually creating
  14. # a new Resource instance within the given Namespace.
  15. #
  16. # The instance of the current resource is available in ResourceController and views
  17. # by calling the #active_admin_config method.
  18. #
  19. class Resource
  20. # Event dispatched when a new resource is registered
  21. RegisterEvent = 'active_admin.resource.register'.freeze
  22. # The namespace this config belongs to
  23. attr_reader :namespace
  24. # The name of the resource class
  25. attr_reader :resource_class_name
  26. # An array of member actions defined for this resource
  27. attr_reader :member_actions
  28. # An array of collection actions defined for this resource
  29. attr_reader :collection_actions
  30. # The default sort order to use in the controller
  31. attr_accessor :sort_order
  32. # Scope this resource to an association in the controller
  33. attr_accessor :scope_to
  34. # If we're scoping resources, use this method on the parent to return the collection
  35. attr_accessor :scope_to_association_method
  36. # Set the configuration for the CSV
  37. attr_writer :csv_builder
  38. module Base
  39. def initialize(namespace, resource_class, options = {})
  40. @namespace = namespace
  41. @resource_class_name = "::#{resource_class.name}"
  42. @options = default_options.merge(options)
  43. @sort_order = @options[:sort_order]
  44. @member_actions, @collection_actions = [], []
  45. end
  46. end
  47. include Base
  48. include Controllers
  49. include PagePresenters
  50. include Pagination
  51. include ActionItems
  52. include Naming
  53. include Scopes
  54. include Sidebars
  55. include Menu
  56. # The class this resource wraps. If you register the Post model, Resource#resource_class
  57. # will point to the Post class
  58. def resource_class
  59. ActiveSupport::Dependencies.constantize(resource_class_name)
  60. end
  61. def resource_table_name
  62. resource_class.quoted_table_name
  63. end
  64. def resource_quoted_column_name(column)
  65. resource_class.connection.quote_column_name(column)
  66. end
  67. # Returns the named route for an instance of this resource
  68. def route_instance_path
  69. [route_prefix, controller.resources_configuration[:self][:route_instance_name], 'path'].compact.join('_').to_sym
  70. end
  71. # Returns a symbol for the route to use to get to the
  72. # collection of this resource
  73. def route_collection_path
  74. route = super
  75. # Handle plural resources.
  76. if controller.resources_configuration[:self][:route_collection_name] ==
  77. controller.resources_configuration[:self][:route_instance_name]
  78. route = route.to_s.gsub('_path', '_index_path').to_sym
  79. end
  80. route
  81. end
  82. # Clears all the member actions this resource knows about
  83. def clear_member_actions!
  84. @member_actions = []
  85. end
  86. def clear_collection_actions!
  87. @collection_actions = []
  88. end
  89. # Are admin notes turned on for this resource
  90. def admin_notes?
  91. admin_notes.nil? ? ActiveAdmin.admin_notes : admin_notes
  92. end
  93. def belongs_to(target, options = {})
  94. @belongs_to = Resource::BelongsTo.new(self, target, options)
  95. controller.belongs_to(target, options.dup)
  96. end
  97. def belongs_to_config
  98. @belongs_to
  99. end
  100. # Do we belong to another resource
  101. def belongs_to?
  102. !belongs_to_config.nil?
  103. end
  104. def include_in_menu?
  105. super && !(belongs_to? && !belongs_to_config.optional?)
  106. end
  107. # The csv builder for this resource
  108. def csv_builder
  109. @csv_builder || default_csv_builder
  110. end
  111. # @deprecated
  112. def resource
  113. resource_class
  114. end
  115. ActiveAdmin::Deprecation.deprecate self, :resource,
  116. "ActiveAdmin::Resource#resource is deprecated. Please use #resource_class instead."
  117. private
  118. def default_options
  119. {
  120. :sort_order => "#{resource_class.respond_to?(:primary_key) ? resource_class.primary_key : 'id'}_desc"
  121. }
  122. end
  123. def default_csv_builder
  124. @default_csv_builder ||= CSVBuilder.default_for_resource(resource_class)
  125. end
  126. end # class Resource
  127. end # module ActiveAdmin