Module: Faalis::Dashboard::Sections::ResourceCreate

Extended by:
ActiveSupport::Concern
Included in:
DSL
Defined in:
lib/faalis/dashboard/sections/resource_create.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#createObject

The actual action method for creating new resource.


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 71

def create
  authorize model

  @resource = model.new(creation_params)
  @resource.assign_attributes(**reflections_hash) unless reflections_hash.nil?

  # Customize the create behaviour
  before_create_hook(@resource)

  # TODO: Handle M2M relations in here
  if @resource.save
    after_create_hook(@resource)
    successful_response(:create)
  else
    errorful_resopnse(:create, @resource.errors) do
      redirect_to Rails.application.routes.url_helpers.send(@new_route), turbolinks: true
    end
  end
end

#editObject


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 25

def edit
  # TODO: Handle nested resource in here
  @resource = model.find(params[:id])
  authorize @resource, :update?

  collect_model_fields_for_form

  @resource_title     = t(_resource_title.singularize)
  @_fields_properties = form_properties._field_details
  @multipart          = has_attachment?

  edit_hook(@resource)

  return if _override_views.include? :edit
  render 'faalis/dashboard/resource/edit'
end

#newObject

The actual action method of a dashboard controller


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 10

def new
  authorize model, :create?

  collect_model_fields_for_form

  @resource           = model.new
  @resource_title     = t(_resource_title.singularize)
  @_fields_properties = form_properties._field_details
  @multipart          = has_attachment?

  new_hook(@resource)
  return if _override_views.include? :new
  render 'faalis/dashboard/resource/new'
end

#updateObject


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 42

def update
  # TODO: Handle nested resource in here
  @resource = model.find(params[:id])
  authorize @resource

  new_params = creation_params
  new_params.merge(reflections_hash) if reflections_hash

  # Customize update behaviour before updating resource
  before_update_hook(@resource)

  # TODO: Move this method to a suitable place instead of controller
  #       itself
  new_params = symbolify_keys(new_params)

  @resource.assign_attributes(**new_params)

  if @resource.save
    # Customize update behaviour after updating resource
    after_update_hook(@resource)
    successful_response(:update)
  else
    errorful_resopnse(:update, @resource.errors) do
      redirect_to :edit
    end
  end
end