Class: Cathode::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from ResourceDsl

#_resources

Methods included from ActionDsl

#actions, #custom_actions, #default_actions

Constructor Details

#initialize(resource_name, params = nil, context = nil, &block) ⇒ Resource

Creates a new resource.

Parameters:

  • resource_name (Symbol)

    The resource’s name

  • params (Hash) (defaults to: nil)

    An optional params hash, e.g. ‘{ actions: :all }`

  • context (Resource) (defaults to: nil)

    An optional parent resource

  • block

    The resource’s actions and properties, defined with the ActionDsl and Cathode::ResourceDsl



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_prefixObject (readonly)

Returns the value of attribute controller_prefix.



8
9
10
# File 'lib/cathode/resource.rb', line 8

def controller_prefix
  @controller_prefix
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/cathode/resource.rb', line 8

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/cathode/resource.rb', line 8

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/cathode/resource.rb', line 8

def parent
  @parent
end

#singularObject (readonly)

Returns the value of attribute singular.



8
9
10
# File 'lib/cathode/resource.rb', line 8

def singular
  @singular
end

#strong_paramsObject (readonly)

Returns the value of attribute strong_params.



8
9
10
# File 'lib/cathode/resource.rb', line 8

def strong_params
  @strong_params
end