Module: Locomotive::API::Helpers::PersistenceHelper

Defined in:
app/api/locomotive/api/helpers/persistence_helper.rb

Overview

Sets up model/repository methods. def translation_params

permitted_params[:translation]

end

def auth(meth)

authorize Translation, meth

end

def translations

current_site.translations

end

def translation

@translation ||= translations.find(params[:id])

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resource_nameObject

Returns the value of attribute resource_name.



26
27
28
# File 'app/api/locomotive/api/helpers/persistence_helper.rb', line 26

def resource_name
  @resource_name
end

#use_form_objectObject

Returns the value of attribute use_form_object.



26
27
28
# File 'app/api/locomotive/api/helpers/persistence_helper.rb', line 26

def use_form_object
  @use_form_object
end

Instance Method Details

#auth(meth) ⇒ Object



60
61
62
# File 'app/api/locomotive/api/helpers/persistence_helper.rb', line 60

def auth(meth)
  authorize persistence_klass, meth
end

#current_policy(obj = nil) ⇒ Class

Returns a class constant for the current Pundit policy on the object.

Parameters:

  • the (Object)

    object to send to the policy class. Leave empty for the singular (default) object to be sent.

Returns:

  • (Class)

    a class constant for the current Pundit policy on the object.



47
48
49
50
# File 'app/api/locomotive/api/helpers/persistence_helper.rb', line 47

def current_policy(obj = nil)
  obj = obj ? obj : send(singular)
  policy_klass.new(pundit_user, obj)
end

#form_klassClass

Returns a class constant for the matching form class.

Returns:

  • (Class)

    a class constant for the matching form class.



40
41
42
# File 'app/api/locomotive/api/helpers/persistence_helper.rb', line 40

def form_klass
  namespaced_klass("#{singular}_form")
end

#persist_from_form(form_object) ⇒ Object

Note:

if the object exists it will be updated

Parameters:

  • an (Object)

    object that responds to #serializable_hash is fed into the persistence class and save is called.



55
56
57
58
# File 'app/api/locomotive/api/helpers/persistence_helper.rb', line 55

def persist_from_form(form_object)
  send(singular).assign_attributes(form_object.serializable_hash)
  send(singular).save!
end

#setup_resource_methods_for(resource_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'app/api/locomotive/api/helpers/persistence_helper.rb', line 28

def setup_resource_methods_for(resource_name)
  self.resource_name = resource_name.to_s

  setup_plural_method
  setup_singular_getter_method
  setup_singular_setter_method
  setup_params_method

  setup_object_auth_method
end