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

- (Object) _new_form_fields (private)



175
176
177
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 175

def _new_form_fields
  all_valid_columns_for_form.keys.map(&:to_sym)
end

- (Object) all_valid_columns_for_form (private)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 144

def all_valid_columns_for_form
  return @all_valid_columns_for_form unless @all_valid_columns_for_form.nil?
  columns   = model.columns_hash.dup
  relations = model.reflections

  relations.keys.each do |name|
    has_many = ActiveRecord::Reflection::HasManyReflection
    has_and_belongs_to_many = ActiveRecord::Reflection::HasAndBelongsToManyReflection

    unless relations[name].is_a? has_many
      if relations[name].is_a? has_and_belongs_to_many
        columns[name] = Class.new { def self.type() :multiple_select end }
      else
        col    = relations[name]
        column = columns.delete col.foreign_key
        columns[name] = column
      end
    end
  end

  columns.delete('id')
  columns.delete('created_at')
  columns.delete('updated_at')

  @all_valid_columns_for_form = columns
end

- (Object) before_create_hook(resource) (private)

You can override this method to change the behaviour of create action



126
127
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 126

def before_create_hook(resource)
end

- (Object) before_update_hook(resource) (private)

You can override this method to change the behaviour of update action



131
132
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 131

def before_update_hook(resource)
end

- (Object) collect_model_fields_for_form (protected)



88
89
90
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 88

def collect_model_fields_for_form
  @_fields ||= form_properties.fields_for_form
end

- (Object) create

The actual action method for creating new resource.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 67

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
    successful_response(:create)
  else
    errorful_resopnse(:create)
  end
end

- (Object) creation_params (protected)



111
112
113
114
115
116
117
118
119
120
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 111

def creation_params
  resource = model_name.underscore.to_sym

  # TODO: replace this line with a better solution to not
  #       allowing the blacklisted fields like id, created_at and ...
  fields = model.columns_hash.keys.map(&:to_sym)
  fields.concat(attachment_fields.map(&:to_sym ))
  fields = Set.new(fields).map(&:to_sym)
  params.require(resource).permit(*fields)
end

- (Object) edit



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     = _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

- (Object) edit_hook(resource) (private)

You can override this method to change the behaviour of edit action



141
142
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 141

def edit_hook(resource)
end

- (Object) form_properties (private)



171
172
173
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 171

def form_properties
  Faalis::Dashboard::DSL::Create.new(model)
end

- (Object) new

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     = _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

- (Object) new_hook(resource) (private)

You can override this method to change the behaviour of new action



136
137
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 136

def new_hook(resource)
end

- (Object) reflections_hash (protected)



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/faalis/dashboard/sections/resource_create.rb', line 92

def reflections_hash
  # TODO: cach the result using and instance_var or something
  reflections = model.reflections
  result      = {}

  reflections.each do |name, column|
    has_many = ActiveRecord::Reflection::HasManyReflection
    has_and_belongs_to_many = ActiveRecord::Reflection::HasAndBelongsToManyReflection

    if !column.is_a?(has_many) && !column.is_a?(has_and_belongs_to_many)
      value = creation_params[column.foreign_key]
      result[column.foreign_key.to_sym] = value
    end
  end

  return result unless result.empty?
  nil
end

- (Object) update



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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_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
    successful_response(:update)
  else
    errorful_resopnse(:update)
  end
end