Class: Trust::Controller::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/trust/controller/properties.rb

Overview

Trust Coontroller Properties

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, properties) ⇒ Properties

:nodoc:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/trust/controller/properties.rb', line 37

def initialize(controller, properties) #:nodoc:
  @controller = controller
  @model = controller.controller_path
  if properties
    @associations = properties.associations.dup
    @new_actions = properties.new_actions.dup
    @member_actions = properties.member_actions.dup
    @collection_actions = properties.collection_actions.dup
  else
    @associations = {}
    @new_actions = [:new, :create]
    @member_actions = [:show, :edit, :update, :destroy]
    @collection_actions = [:index]
  end
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



32
33
34
# File 'lib/trust/controller/properties.rb', line 32

def associations
  @associations
end

#collection_actionsObject

Returns the value of attribute collection_actions.



35
36
37
# File 'lib/trust/controller/properties.rb', line 35

def collection_actions
  @collection_actions
end

#controllerObject (readonly)

Returns the value of attribute controller.



30
31
32
# File 'lib/trust/controller/properties.rb', line 30

def controller
  @controller
end

#member_actionsObject

Returns the value of attribute member_actions.



34
35
36
# File 'lib/trust/controller/properties.rb', line 34

def member_actions
  @member_actions
end

#model(name = nil) ⇒ Object

Returns or sets the model to be used in a controller

If not set, the controller_path is used. You can override the model to be accessed in a controller by setting the model. Note that you should specify the model in plural form.

Example

# You have a controller which inherits from a generic controller and it has not the same name. Below
model :accounts # will assume that the class to be Account and instance variables to be @account/@accounts

# name spaced models
model :"customer/accounts"


76
77
78
# File 'lib/trust/controller/properties.rb', line 76

def model
  @model
end

#new_actionsObject

Returns the value of attribute new_actions.



33
34
35
# File 'lib/trust/controller/properties.rb', line 33

def new_actions
  @new_actions
end

Class Method Details

.instantiate(controller) ⇒ Object

Returns a controller properties object.

Ensures controller properties are instantiated in a correct manner and that inheritance is supported



57
58
59
# File 'lib/trust/controller/properties.rb', line 57

def instantiate(controller)
  new(controller, controller.superclass.instance_variable_get(:@properties))
end

Instance Method Details

#actions(options) ⇒ Object

Specify actions to handle

Options

:new => actions        # specify new actions - default id :new, :create
:member => actions     # specify member actions - default is :show, :edit, :update, :destroy
:collection => actions # specify collection actions - default is :index
:except => actions     # removes any standard actions
:only => actions       # selects only the standard actions specifiec
:add => {options}      # to add options, eg  :add => {:new => :confirm}


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/trust/controller/properties.rb', line 130

def actions(options)
  if add = options[:add]
    self.new_actions += Array.wrap(add[:new]) if add[:new]
    self.member_actions += Array.wrap(add[:member]) if add[:member]
    self.collection_actions += Array.wrap(add[:collection]) if add[:collection]
  end
  self.new_actions = Array.wrap(options[:new]) if options[:new]
  self.member_actions = Array.wrap(options[:member]) if options[:member]
  self.collection_actions = Array.wrap(options[:collection]) if options[:collection]
  if options[:only]
    only = Array.wrap(options[:only])
    self.new_actions &= only
    self.member_actions &= only
    self.collection_actions &= only
  end
  if options[:except]
    except = Array.wrap(options[:except])
    self.new_actions -= except
    self.member_actions -= except
    self.collection_actions -= except
  end
end

#belongs_to(*resources) ⇒ Object

Specify associated resources (nested resources)

Example

+belongs_to+ :lottery
+belongs_to+ :table, :card_game
+belongs_to+ :card_game, :as => :bridge

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
# File 'lib/trust/controller/properties.rb', line 106

def belongs_to(*resources)
  raise ArgumentError, "You must specify at least one resource after belongs_to" unless resources
  logger.debug "#{@model} belongs_to #{resources.inspect}"
  options = resources.extract_options!
  resources.each do |resource|
    @associations[resource] = options[:as]
  end
end

#collection_action?(action) ⇒ Boolean

> true if action is a collection_action

Returns:

  • (Boolean)


91
92
93
# File 'lib/trust/controller/properties.rb', line 91

def collection_action?(action)
  collection_actions.include? action.to_sym
end

#has_associations?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/trust/controller/properties.rb', line 115

def has_associations?
  @associations.size > 0
end

#member_action?(action) ⇒ Boolean

> true if action is a member_action

Returns:

  • (Boolean)


95
96
97
# File 'lib/trust/controller/properties.rb', line 95

def member_action?(action)
  member_actions.include? action.to_sym
end

#model_classObject

Returns the class for the model



82
83
84
# File 'lib/trust/controller/properties.rb', line 82

def model_class
  model.to_s.classify.constantize
end

#new_action?(action) ⇒ Boolean

> true if action is a new_action

Returns:

  • (Boolean)


87
88
89
# File 'lib/trust/controller/properties.rb', line 87

def new_action?(action)
  new_actions.include? action.to_sym
end