Module: SkinnyControllers::Operation::ModelHelpers

Included in:
Base
Defined in:
lib/skinny_controllers/operation/model_helpers.rb

Instance Method Summary collapse

Instance Method Details

#find_modelObject

hopefully this method is only ever called once per request via the memoization in #model



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 12

def find_model
  if params[:scope]
    model_from_scope
  elsif (key = params.keys.grep(/\_id$/)).present?
    # hopefully there is only ever one of these passed
    id = params[key.first]
    if params['id'].present?
      # single item / show
      model_from_parent(key.first, id, params['id'])
    else
      # list of items / index
      model_from_named_id(key.first, id)
    end
  elsif id_from_params
    model_from_id
  else
    model_from_params
  end
end

#modelObject



4
5
6
7
8
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 4

def model
  # TODO: not sure if multiple ids is a good idea here
  # if we don't have a(ny) id(s), get all of them
  @model ||= find_model
end

#model_from_idObject



107
108
109
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 107

def model_from_id
  model_class.find(id_from_params)
end

#model_from_named_id(key, id) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 84

def model_from_named_id(key, id)
  name = key.gsub(/_id$/, '')
  name = name.camelize
  model_from_scope(
    id: id,
    type: name
  )
end

#model_from_paramsObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 71

def model_from_params
  ar_proxy = model_class.where(sanitized_params)

  if ar_proxy.respond_to? SkinnyControllers.accessible_to_scope
    # It's better to filter in sql, than in the app, so if there is
    # a way to do the filtering in active query, do that. This will help
    # mitigate n+1 query scenarios
    return ar_proxy.send(SkinnyControllers.accessible_to_scope, current_user)
  end

  ar_proxy
end

#model_from_parent(parent_class, parent_id, id) ⇒ Object



102
103
104
105
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 102

def model_from_parent(parent_class, parent_id, id)
  association = model_from_named_id(parent_class, parent_id)
  association.find(id)
end

#model_from_scope(scope = params[:scope]) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 93

def model_from_scope(scope = params[:scope])
  if scoped = scoped_model(scope)
    association = association_name_from_object
    scoped.send(association)
  else
    fail "Parent object of type #{scope[:type]} not accessible"
  end
end

#model_param_nameObject



51
52
53
54
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 51

def model_param_name
  # model_key comes from Operation::Base
  self.model_key || model_name.underscore
end

#model_paramsObject

TODO: add a way to use existing strong parameters methods



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 38

def model_params
  # for mass-assignment, rails doesn't accept
  # stringified keys.
  # TODO: why did the params hash lose its indifferent access
  unless @model_params
    model_params = (params_for_action[model_param_name] || params_for_action)

    @model_params = (model_params == params) ? {} : model_params.symbolize_keys
  end

  @model_params
end

#sanitized_paramsObject



32
33
34
35
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 32

def sanitized_params
  keys = (model_class.column_names & params.keys)
  params.slice(*keys).symbolize_keys
end

#scoped_model(scoped_params) ⇒ Object

Options Hash (scoped_params):

  • :type (Object)

    the class name

  • :id (Object)

    the id of the class to look up



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 59

def scoped_model(scoped_params)
  unless @scoped_model
    klass_name = scoped_params[:type]
    operation_class = Lookup::Operation.operation_of(klass_name, DefaultVerbs::Read)
    operation = operation_class.new(current_user, id: scoped_params[:id])
    @scoped_model = operation.run
    self.authorized_via_parent = !!@scoped_model
  end

  @scoped_model
end