Module: ControllerSetterPattern::ActionController::ClassMethods

Defined in:
lib/controller_setter_pattern/action_controller.rb

Instance Method Summary collapse

Instance Method Details

#set(*names) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/controller_setter_pattern/action_controller.rb', line 6

def set(*names)
  options = names.extract_options!

  model = options.delete(:model)
  model = model.to_s.camelize.constantize unless model.nil?

  finder_params = normalize_finder_params(options.delete(:finder_params) || [])
  finder_method = 'find' + (finder_params.empty? ? '' : '_by_' + finder_params.join('_and_'))

  ancestor = options.delete(:ancestor)
  scope = options.delete(:scope)

  names.each do |variable|
    before_action options do |controller|
      if !ancestor.nil?
        if controller.instance_variable_defined?("@#{ancestor}")
          resource = controller.instance_variable_get("@#{ancestor}")
          model_class = resource.class
        else
          model_class = ancestor.to_s.camelize.constantize
          resource = model_class.find(params["#{model_class.name.underscore}_id".to_sym])
        end
        reflection_name = (model || variable).to_s.underscore.pluralize.to_sym
        resource = resource.send(reflection_name) if resource.respond_to?(reflection_name)
      elsif !model.nil? && model.descends_from_active_record?
        model_class = model
      elsif
        model_class = variable.to_s.camelize.constantize
      end

      resource ||= model_class

      unless scope.nil?
        scope = [scope] unless scope.is_a?(Array)
        scope.each { |s| resource = resource.send(s) }
      end

      if finder_params.empty?
        values_for_find = controller.params["#{variable}_id".to_sym] || controller.params[:id]
      else
        defaults = finder_params.inject({}) { |h, v| h.merge(v.to_s => nil) }
        values_for_find = defaults.merge(controller.params.permit(finder_params)).values
      end

      resource = resource.send(finder_method, *values_for_find)

      var_name = "@#{variable}".to_sym
      controller.instance_variable_set(var_name, resource)
    end
  end
end