Class: Para::Admin::ResourcesController

Inherits:
BaseController show all
Includes:
Helpers::AttributesMappings, ModelHelper
Defined in:
app/controllers/para/admin/resources_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::AttributesMappings

#attributes_mappings_for

Methods included from ModelHelper

#attribute_field_mappings_for, #excerpt_value_for, #field_for, #field_value_for, #model_field_mappings, #relation_klass_for, #value_for

Methods inherited from BaseController

cancan_resource_class, #current_ability, #current_admin, load_and_authorize_component

Methods included from BaseHelper

#find_partial_for, #find_relation_name_for, #flash_message, #registered_components_options, #resource_title_for, #template_path_lookup

Methods included from Helpers::ResourceName

#resource_name

Methods inherited from Para::ApplicationController

#admin?

Class Method Details

.ensure_resource_name_defined!Object



167
168
169
170
171
172
173
174
# File 'app/controllers/para/admin/resources_controller.rb', line 167

def self.ensure_resource_name_defined!
  return if resource_name.presence

  raise 'Resource not defined in your controller. ' \
        'You can define the resource of your controller with the ' \
        '`resource :resource_name` macro when subclassing ' \
        'Para::Admin::ResourcesController'
end

.resource(name, options = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/para/admin/resources_controller.rb', line 144

def self.resource(name, options = {})
  default_options = {
    class: name.to_s.camelize,
    parent: false
  }

  default_options.each do |key, value|
    options[key] = value unless options.key?(key)
  end

  self.resource_name = name
  self.resource_class = options[:class]

  load_and_authorize_resource(name, options)
end

.resource_modelObject



160
161
162
163
164
165
# File 'app/controllers/para/admin/resources_controller.rb', line 160

def self.resource_model
  @resource_model ||= begin
    ensure_resource_name_defined!
    Para.const_get(resource_class)
  end
end

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/para/admin/resources_controller.rb', line 17

def create
  # Assign component the resource belongs to it
  resource.component = @component if resource.respond_to?(:component=)

  # We overriden Cancan so it does not try to assign our params during the
  # create action, allowing our attribute fields to parse them with an
  # actual resource.
  resource.assign_attributes(resource_params)

  if resource.save
    flash_message(:success, resource)
    redirect_to after_form_submit_path, status: 303
  else
    flash_message(:error, resource)
    render 'new', status: 422
  end
end

#destroyObject



49
50
51
52
53
# File 'app/controllers/para/admin/resources_controller.rb', line 49

def destroy
  resource.destroy
  flash_message(:success, resource)
  redirect_to after_form_submit_path, status: 303
end

#editObject



35
36
37
# File 'app/controllers/para/admin/resources_controller.rb', line 35

def edit
  render 'para/admin/resources/edit'
end

#newObject



13
14
15
# File 'app/controllers/para/admin/resources_controller.rb', line 13

def new
  render 'para/admin/resources/new'
end

#orderObject



55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/para/admin/resources_controller.rb', line 55

def order
  ActiveRecord::Base.transaction do
    resources_data.each do |resource_params|
      resource = resources_hash[resource_params[:id]]
      resource.position = resource_params[:position].to_i
      resource.save(validate: false)
    end
  end

  head 200
end

#treeObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/para/admin/resources_controller.rb', line 67

def tree
  ActiveRecord::Base.transaction do
    resources_data.each do |resource_params|
      resource = resources_hash[resource_params[:id]]
      resource.assign_attributes(
        position: resource_params[:position].to_i,
        parent_id: resource_params[:parent_id]
      )
      resource.save(validate: false)
    end
  end

  head 200
end

#updateObject



39
40
41
42
43
44
45
46
47
# File 'app/controllers/para/admin/resources_controller.rb', line 39

def update
  if resource.update(resource_params)
    flash_message(:success, resource)
    redirect_to after_form_submit_path, status: 303
  else
    flash_message(:error, resource)
    render 'edit', status: 422
  end
end