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



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

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



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

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
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 84

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

  instance_exec(association, &SkinnyControllers.search_proc)
end

#model_from_paramsObject



80
81
82
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 80

def model_from_params
  model_class.where(sanitized_params)
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



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

def model_from_scope(scope = params[:scope])
  if scoped = scoped_model(scope)
    association = association_name_from_object

    scoped.send(association)
  else
    raise ActiveRecord::RecordNotFound, "Parent object of type #{scope[:type]} not accessible"
  end
end

#model_param_nameObject



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

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

#model_paramsObject

TODO: add a way to use existing strong parameters methods



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

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



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

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

#scoped_model(scoped_params) ⇒ Object

Parameters:

  • scoped_params (Hash)

Options Hash (scoped_params):

  • :type (Object)

    the class name

  • :id (Object)

    the id of the class to look up



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/skinny_controllers/operation/model_helpers.rb', line 60

def scoped_model(scoped_params)
  unless @scoped_model
    klass_name = scoped_params[:type]
    operation_class = Lookup::Operation.operation_of(klass_name, DefaultVerbs::Read)

    params = { id: scoped_params[:id] }
    operation = operation_class.new(
      current_user,
      params, params,
      'show', nil,
      model_class: klass_name.safe_constantize
    )

    @scoped_model = operation.run
    self.authorized_via_parent = !!@scoped_model
  end

  @scoped_model
end