Class: Puffer::Resource

Inherits:
Object
  • Object
show all
Includes:
Routing
Defined in:
lib/puffer/resource.rb,
lib/puffer/resource/node.rb,
lib/puffer/resource/tree.rb,
lib/puffer/resource/routing.rb

Overview

Resource is presenter layer for controllers.

Defined Under Namespace

Modules: Routing Classes: Node, Tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Routing

#collection_method, #default_url_options, #edit_method, #member_method, #new_method, #url_arguments, #url_method_name, #use_resource_id

Constructor Details

#initialize(params, controller_instance = nil) ⇒ Resource

Returns a new instance of Resource.



18
19
20
21
22
23
24
25
# File 'lib/puffer/resource.rb', line 18

def initialize params, controller_instance = nil
  params = ActiveSupport::HashWithIndifferentAccess.new.deep_merge params

  @resource_node = params[:puffer]
  @scope = swallow_nil{@resource_node.scope} || controller_instance.puffer_namespace
  @params = params
  @controller_instance = controller_instance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



140
141
142
# File 'lib/puffer/resource.rb', line 140

def method_missing method, *args, &block
  model.send method, *args, &block if model.respond_to? method
end

Instance Attribute Details

#controller_instanceObject (readonly)

Returns the value of attribute controller_instance.



13
14
15
# File 'lib/puffer/resource.rb', line 13

def controller_instance
  @controller_instance
end

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/puffer/resource.rb', line 13

def params
  @params
end

#resource_nodeObject (readonly)

Returns the value of attribute resource_node.



13
14
15
# File 'lib/puffer/resource.rb', line 13

def resource_node
  @resource_node
end

#scopeObject (readonly)

Returns the value of attribute scope.



13
14
15
# File 'lib/puffer/resource.rb', line 13

def scope
  @scope
end

Instance Method Details

#adapterObject



95
96
97
# File 'lib/puffer/resource.rb', line 95

def adapter
  model.to_adapter
end

#ancestorsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/puffer/resource.rb', line 31

def ancestors
  @ancestors ||= begin
    ancestors = []
    resource = self
    while resource = resource.parent do
      ancestors.unshift resource
    end
    ancestors
  end
end

#attributesObject



132
133
134
# File 'lib/puffer/resource.rb', line 132

def attributes
  params[attributes_key] || {}
end

#attributes_keyObject



136
137
138
# File 'lib/puffer/resource.rb', line 136

def attributes_key
  model.model_name.param_key
end

#childrenObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puffer/resource.rb', line 64

def children
  @children ||= resource_node.children.map do |child_node|
    child_params = ActiveSupport::HashWithIndifferentAccess.new({:puffer => child_node})

    resource_node.ancestors.each do |ancestor|
      key = ancestor.to_s.singularize.foreign_key
      child_params[key] = params[key] if params[key]
    end

    key = resource_node.to_s.singularize.foreign_key
    child_params[key] = params[:id] if params[:id]

    self.class.new child_params, controller_instance
  end
end

#children_hashObject



80
81
82
83
84
85
# File 'lib/puffer/resource.rb', line 80

def children_hash
  @children_hash ||= children.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, child|
    result[child.name] = child
    result
  end
end

#collectionObject



99
100
101
102
103
104
105
106
# File 'lib/puffer/resource.rb', line 99

def collection
  scope = collection_scope
  scope = scope.send controller.configuration.scope if controller.configuration.scope
  adapter.filter scope, controller.filter_fields,
    :conditions => controller_instance.puffer_filters.conditions,
    :search => controller_instance.puffer_filters.search,
    :order => controller_instance.puffer_filters.order
end

#collection_scopeObject



91
92
93
# File 'lib/puffer/resource.rb', line 91

def collection_scope
  parent ? parent.member.send(name) : model
end

#humanObject



27
28
29
# File 'lib/puffer/resource.rb', line 27

def human
  model.model_name.human
end

#memberObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/puffer/resource.rb', line 108

def member
  if parent
    if plural?
      parent.member.send(name).find member_id if member_id
    else
      parent.member.send(name)
    end
  else
    adapter.get member_id if member_id
  end
end

#member_idObject



87
88
89
# File 'lib/puffer/resource.rb', line 87

def member_id
  params[:id]
end

#new_member(attributes = attributes) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/puffer/resource.rb', line 120

def new_member attributes = attributes
  if parent
    if plural?
      parent.member.send(name).new attributes
    else
      parent.member.send("build_#{name}", attributes)
    end
  else
    model.new attributes
  end
end

#parentObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puffer/resource.rb', line 46

def parent
  @parent ||= begin
    if resource_node.parent
      parent_params = ActiveSupport::HashWithIndifferentAccess.new({:puffer => resource_node.parent})

      resource_node.ancestors[0..-2].each do |ancestor|
        key = ancestor.to_s.singularize.foreign_key
        parent_params[key] = params[key] if params[key]
      end

      key = resource_node.parent.to_s.singularize.foreign_key
      parent_params[:id] = params[key] if params[key]

      self.class.new parent_params, controller_instance
    end
  end
end

#rootObject



42
43
44
# File 'lib/puffer/resource.rb', line 42

def root
  @root ||= (ancestors.first || self)
end