Class: Cathode::Resource
- Inherits:
-
Object
- Object
- Cathode::Resource
- Includes:
- ActionDsl, ResourceDsl
- Defined in:
- lib/cathode/resource.rb
Overview
A ‘Resource` is paired with a Rails model and describes the actions that the resource responds to.
Instance Attribute Summary collapse
-
#controller_prefix ⇒ Object
readonly
Returns the value of attribute controller_prefix.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#singular ⇒ Object
readonly
Returns the value of attribute singular.
-
#strong_params ⇒ Object
readonly
Returns the value of attribute strong_params.
Instance Method Summary collapse
-
#initialize(resource_name, params = nil, context = nil, &block) ⇒ Resource
constructor
Creates a new resource.
Methods included from ResourceDsl
Methods included from ActionDsl
#actions, #custom_actions, #default_actions
Constructor Details
#initialize(resource_name, params = nil, context = nil, &block) ⇒ Resource
Creates a new resource.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cathode/resource.rb', line 21 def initialize(resource_name, params = nil, context = nil, &block) require_resource_constant! resource_name params ||= {} params[:actions] ||= [] @name = resource_name camelized_resource = resource_name.to_s.camelize @controller_prefix = if context.present? && context.is_a?(Resource) @parent = context "#{context.controller_prefix}#{camelized_resource}" else camelized_resource end controller_name = "#{@controller_prefix}Controller" unless Cathode.const_defined? controller_name Cathode.const_set controller_name, Class.new(Cathode::BaseController) end @actions = ObjectCollection.new actions_to_add = params[:actions] == :all ? DEFAULT_ACTIONS : params[:actions] actions_to_add.each { |action_name| action action_name } @singular = params[:singular] instance_eval(&block) if block_given? @actions.each do |action| action.after_resource_initialized if action.respond_to? :after_resource_initialized end if @strong_params.present? && actions.find(:create).nil? && actions.find(:update).nil? raise UnknownActionError, 'An attributes block was specified without a :create or :update action' end end |
Instance Attribute Details
#controller_prefix ⇒ Object (readonly)
Returns the value of attribute controller_prefix.
8 9 10 |
# File 'lib/cathode/resource.rb', line 8 def controller_prefix @controller_prefix end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
8 9 10 |
# File 'lib/cathode/resource.rb', line 8 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/cathode/resource.rb', line 8 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
8 9 10 |
# File 'lib/cathode/resource.rb', line 8 def parent @parent end |
#singular ⇒ Object (readonly)
Returns the value of attribute singular.
8 9 10 |
# File 'lib/cathode/resource.rb', line 8 def singular @singular end |
#strong_params ⇒ Object (readonly)
Returns the value of attribute strong_params.
8 9 10 |
# File 'lib/cathode/resource.rb', line 8 def strong_params @strong_params end |