Class: Avo::BaseController

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

Direct Known Subclasses

AssociationsController, ResourcesController

Instance Method Summary collapse

Methods inherited from ApplicationController

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

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, #root_path_without_url, #svg

Instance Method Details

#createObject



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
# File 'app/controllers/avo/base_controller.rb', line 122

def create
  # model gets instantiated and filled in the fill_model method
  saved = save_model
  @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

    # Fills in the required infor for belongs_to and 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)
      @model.send("#{@reflection.foreign_key}=", params[:via_resource_id])
      @model.save
    end

    # For when working with 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

  add_breadcrumb @resource.plural_name.humanize, resources_path(resource: @resource)
  add_breadcrumb t("avo.new").humanize
  set_actions

  if saved
    create_success_action
  else
    create_fail_action
  end
end

#destroyObject



177
178
179
180
181
182
183
# File 'app/controllers/avo/base_controller.rb', line 177

def destroy
  if destroy_model
    destroy_success_action
  else
    destroy_fail_action
  end
end

#editObject



160
161
162
# File 'app/controllers/avo/base_controller.rb', line 160

def edit
  set_actions
end

#indexObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/avo/base_controller.rb', line 16

def index
  @page_title = @resource.plural_name.humanize
  add_breadcrumb @resource.plural_name.humanize

  set_index_params
  set_filters
  set_actions

  # If we don't get a query object predefined from a child controller like associations, 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 associations
  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

    # Check if the sortable field option is actually a proc and we need to do a custom sort
    field_id = @index_params[:sort_by].to_sym
    field = @resource.get_field_definitions.find { |field| field.id == field_id }
    @query = if field&.sortable.is_a?(Proc)
      field.sortable.call(@query, @index_params[:sort_direction])
    else
      @query.order("#{@resource.model_class.table_name}.#{@index_params[:sort_by]} #{@index_params[:sort_direction]}")
    end
  end

  # Apply filters to the current query
  filters_to_be_applied.each do |filter_class, filter_value|
    @query = filter_class.safe_constantize.new.apply_query request, @query, filter_value
  end

  extra_pagy_params = {}

  # Reset open filters when a user navigates to a new page
  extra_pagy_params[:keep_filters_panel_open] = if params[:keep_filters_panel_open] == "1"
    "0"
  end

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

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

#newObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/avo/base_controller.rb', line 101

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

  set_actions

  @page_title = @resource.default_panel_name.to_s

  if params[:via_relation_class].present? && params[:via_resource_id].present?
    via_resource = Avo::App.get_resource_by_model_name params[:via_relation_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)
  end

  add_breadcrumb @resource.plural_name.humanize, resources_path(resource: @resource)
  add_breadcrumb t("avo.new").humanize
end

#orderObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/avo/base_controller.rb', line 185

def order
  direction = params[:direction].to_sym

  if direction.present?
    @resource
      .hydrate(model: @model, params: params)
      .ordering_host
      .order direction
  end

  respond_to do |format|
    format.html { redirect_to params[:referrer] || resources_path(resource: @resource) }
  end
end

#showObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/avo/base_controller.rb', line 78

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

  set_actions

  @page_title = @resource.default_panel_name.to_s

  # 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.plural_name.humanize, resources_path(resource: @resource)
  end

  add_breadcrumb @resource.model_title
  add_breadcrumb I18n.t("avo.details").upcase_first
end

#updateObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/avo/base_controller.rb', line 164

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

  if saved
    update_success_action
  else
    update_fail_action
  end
end