Class: Heimdallr::ResourceImplementation::ResourceLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/heimdallr/resource_implementation.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller, options, loader_options = {}) ⇒ ResourceLoader

Returns a new instance of ResourceLoader.



59
60
61
62
63
64
65
# File 'lib/heimdallr/resource_implementation.rb', line 59

def initialize(controller, options, loader_options = {})
  @restricted = loader_options[:restricted]
  @parent = loader_options[:parent]
  @controller = controller
  @options = options
  @params = controller.params
end

Instance Method Details

#action_typeObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/heimdallr/resource_implementation.rb', line 140

def action_type
  if @parent
    :parent_resource
  else
    case action = @params[:action].to_sym
    when :index
      :collection
    when :new, :create
      :new_resource
    when :show, :edit, :update, :destroy
      :resource
    else
      if @options[:collection] && @options[:collection].include?(action)
        :collection
      elsif @options[:new_record] && @options[:new_record].include?(action)
        :new_resource
      else
        :resource
      end
    end
  end
end

#class_nameObject



177
178
179
# File 'lib/heimdallr/resource_implementation.rb', line 177

def class_name
  resource_name.classify
end

#collection(scope, parent_resource) ⇒ Object



106
107
108
# File 'lib/heimdallr/resource_implementation.rb', line 106

def collection(scope, parent_resource)
  scope
end

#ivar_nameObject



163
164
165
166
167
# File 'lib/heimdallr/resource_implementation.rb', line 163

def ivar_name
  name = (@options[:instance_name] || variable_name).to_s
  name = name.pluralize if action_type == :collection
  :"@#{name}"
end

#loadObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/heimdallr/resource_implementation.rb', line 67

def load
  return @controller.instance_variable_get(ivar_name) if @controller.instance_variable_defined? ivar_name

  if !@parent && @options.has_key?(:through)
    parent_resource = load_parent
    raise "Cannot fetch #{@options[:resource]} through #{@options[:through]}" unless parent_resource || @options[:shallow]
  else
    parent_resource = nil
  end

  resource = self.send action_type, resource_scope(parent_resource), parent_resource
  @controller.instance_variable_set ivar_name, resource unless resource.nil?
  resource
end

#load_parentObject



82
83
84
85
86
# File 'lib/heimdallr/resource_implementation.rb', line 82

def load_parent
  Array.wrap(@options[:through]).map { |parent|
    ResourceLoader.new(@controller, {:resource => parent}, :restricted => @restricted, :parent => true).load
  }.reject(&:nil?).first
end

#new_resource(scope, parent_resource) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/heimdallr/resource_implementation.rb', line 110

def new_resource(scope, parent_resource)
  attributes = @params[params_key_name] || {}

  if @options[:singleton] && parent_resource
    if scope.nil?
      parent_resource.send singleton_builder_name, attributes
    else
      scope.assign_attributes attributes
      scope
    end
  else
    scope.new attributes
  end
end

#params_key_nameObject



181
182
183
# File 'lib/heimdallr/resource_implementation.rb', line 181

def params_key_name
  resource_name.split('/').last
end

#parent_resource(scope, parent_resource) ⇒ Object



134
135
136
137
138
# File 'lib/heimdallr/resource_implementation.rb', line 134

def parent_resource(scope, parent_resource)
  key = @params[:"#{params_key_name}_id"]
  return if key.blank?
  scope.send(@options[:finder] || :find, key)
end

#resource(scope, parent_resource) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/heimdallr/resource_implementation.rb', line 125

def resource(scope, parent_resource)
  if @options[:singleton] && parent_resource
    scope
  else
    key = [:"#{params_key_name}_id", :id].map{|key| @params[key] }.find &:present?
    scope.send(@options[:finder] || :find, key)
  end
end

#resource_nameObject



169
170
171
# File 'lib/heimdallr/resource_implementation.rb', line 169

def resource_name
  @options[:resource].to_s
end

#resource_scope(parent_resource) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/heimdallr/resource_implementation.rb', line 88

def resource_scope(parent_resource)
  if parent_resource
    if @options[:through_association]
      parent_resource.send @options[:through_association]
    elsif @options[:singleton]
      parent_resource.send :"#{variable_name}"
    else
      parent_resource.send :"#{variable_name.pluralize}"
    end
  else
    if @restricted
      class_name.constantize.restrict(@controller.security_context)
    else
      class_name.constantize.scoped
    end
  end
end

#singleton_builder_nameObject



185
186
187
# File 'lib/heimdallr/resource_implementation.rb', line 185

def singleton_builder_name
  :"build_#{@options[:through_association] || variable_name}"
end

#variable_nameObject



173
174
175
# File 'lib/heimdallr/resource_implementation.rb', line 173

def variable_name
  resource_name.parameterize('_')
end