Class: Avo::BaseController

Inherits:
ApplicationController show all
Defined in:
app/controllers/avo/base_controller.rb

Direct Known Subclasses

RelationsController, ResourcesController

Instance Method Summary collapse

Methods inherited from ApplicationController

#_current_user, #check_avo_license, #context, #exception_logger, #init_app, #render

Methods included from UrlHelpers

#edit_resource_path, #new_resource_path, #related_resources_path, #resource_attach_path, #resource_detach_path, #resource_path, #resources_path

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #empty_state, #get_model_class, #input_classes, #render_license_warning, #render_license_warnings, #svg, #turbo_frame_wrap

Instance Method Details

#createObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/avo/base_controller.rb', line 113

def create
  # model gets instantiated and filled in the fill_model method
  fill_model
  saved = @model.save
  @resource.hydrate(model: @model, view: :new, user: _current_user)

  # This means that the record has been created through another parent record and we need to attach it somehow.
  if params[:via_resource_id].present?
    @reflection = @model._reflections[params[:via_relation]]
    # Figure out what kind of association does the record have with the parent record

    # belongs_to
    # has_many
    # Get the foreign key and set it to the id we received in the params
    if @reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection) || @reflection.is_a?(ActiveRecord::Reflection::HasManyReflection)
      foreign_key = @reflection.foreign_key
      @model.send("#{foreign_key}=", params[:via_resource_id])
      @model.save
    end

    # has_one
    # has_one_through

    # has_many_through
    # has_and_belongs_to_many
    # polymorphic
    if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection
      # find the record
      via_resource = ::Avo::App.get_resource_by_model_name params[:via_relation_class]
      @related_record = via_resource.model_class.find params[:via_resource_id]

      @model.send(params[:via_relation]) << @related_record
    end
  end

  respond_to do |format|
    if saved
      redirect_path = resource_path(model: @model, resource: @resource)

      if params[:via_relation_class].present? && params[:via_resource_id].present?
        parent_resource = ::Avo::App.get_resource_by_model_name params[:via_relation_class].safe_constantize
        redirect_path = resource_path(model: params[:via_relation_class].safe_constantize, resource: parent_resource, resource_id: params[:via_resource_id])
      end

      format.html { redirect_to redirect_path, notice: "#{@model.class.name} #{t("avo.was_successfully_created")}." }
      format.json { render :show, status: :created, location: @model }
    else
      flash[:error] = t "avo.you_missed_something_check_form"
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @model.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject



185
186
187
188
189
190
191
192
# File 'app/controllers/avo/base_controller.rb', line 185

def destroy
  @model.destroy!

  respond_to do |format|
    format.html { redirect_to params[:referrer] || resources_path(resource: @resource, turbo_frame: params[:turbo_frame], view_type: params[:view_type]), notice: t("avo.resource_destroyed", attachment_class: @attachment_class) }
    format.json { head :no_content }
  end
end

#editObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/avo/base_controller.rb', line 92

def edit
  @resource = @resource.hydrate(model: @model, view: :edit, user: _current_user)

  @page_title = @resource.default_panel_name

  # If we're accessing this resource via another resource add the parent to the breadcrumbs.
  if params[:via_resource_class].present? && params[:via_resource_id].present?
    via_resource = Avo::App.get_resource_by_model_name params[:via_resource_class]
    via_model = via_resource.class.find_scope.find params[:via_resource_id]
    via_resource.hydrate model: via_model

    add_breadcrumb via_resource.plural_name, resources_path(resource: @resource)
    add_breadcrumb via_resource.model_title, resource_path(model: via_model, resource: via_resource)
  else
    add_breadcrumb resource_name.humanize, resources_path(resource: @resource)
  end

  add_breadcrumb @resource.model_title, resource_path(model: @resource.model, resource: @resource)
  add_breadcrumb t("avo.edit").humanize
end

#indexObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/avo/base_controller.rb', line 14

def index
  @page_title = resource_name.humanize
  add_breadcrumb resource_name.humanize

  set_index_params
  set_filters
  set_actions

  # If we don't get a query object predefined from a child controller like relations, just spin one up
  unless defined? @query
    @query = @resource.class.query_scope
  end

  # Remove default_scope for index view if no parent_resource present
  if @resource.unscoped_queries_on_index && @parent_resource.blank?
    @query = @query.unscoped
  end

  # Eager load the relations
  if @resource.includes.present?
    @query = @query.includes(*@resource.includes)
  end

  # Eager load the active storage attachments
  @query = eager_load_files(@resource, @query)

  # Sort the items
  if @index_params[:sort_by].present?
    unless @index_params[:sort_by].eql? :created_at
      @query = @query.unscope(:order)
    end
    @query = @query.order("#{@resource.model_class.table_name}.#{@index_params[:sort_by]} #{@index_params[:sort_direction]}")
  end

  # Apply filters
  applied_filters.each do |filter_class, filter_value|
    @query = filter_class.safe_constantize.new.apply_query request, @query, filter_value
  end

  @pagy, @models = pagy(@query, items: @index_params[:per_page], link_extra: "data-turbo-frame=\"#{params[:turbo_frame]}\"", size: [1, 2, 2, 1])

  # Create resources for each model
  @resources = @models.map do |model|
    @resource.hydrate(model: model, params: params).dup
  end
end

#newObject



83
84
85
86
87
88
89
90
# File 'app/controllers/avo/base_controller.rb', line 83

def new
  @model = @resource.model_class.new
  @resource = @resource.hydrate(model: @model, view: :new, user: _current_user)

  @page_title = @resource.default_panel_name
  add_breadcrumb resource_name.humanize, resources_path(resource: @resource)
  add_breadcrumb t("avo.new").humanize
end

#showObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/avo/base_controller.rb', line 61

def show
  set_actions

  @resource = @resource.hydrate(model: @model, view: :show, user: _current_user, params: params)

  @page_title = @resource.default_panel_name

  # If we're accessing this resource via another resource add the parent to the breadcrumbs.
  if params[:via_resource_class].present? && params[:via_resource_id].present?
    via_resource = Avo::App.get_resource_by_model_name params[:via_resource_class]
    via_model = via_resource.class.find_scope.find params[:via_resource_id]
    via_resource.hydrate model: via_model

    add_breadcrumb via_resource.plural_name, resources_path(resource: via_resource)
    add_breadcrumb via_resource.model_title, resource_path(model: via_model, resource: via_resource)
  else
    add_breadcrumb resource_name.humanize, resources_path(resource: @resource)
  end

  add_breadcrumb @resource.model_title
end

#updateObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/avo/base_controller.rb', line 167

def update
  # model gets instantiated and filled in the fill_model method
  fill_model
  saved = @model.save
  @resource = @resource.hydrate(model: @model, view: :edit, user: _current_user)

  respond_to do |format|
    if saved
      format.html { redirect_to params[:referrer] || resource_path(model: @model, resource: @resource), notice: "#{@model.class.name} #{t("avo.was_successfully_updated")}." }
      format.json { render :show, status: :ok, location: @model }
    else
      flash[:error] = t "avo.you_missed_something_check_form"
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @model.errors, status: :unprocessable_entity }
    end
  end
end