Class: Avo::BaseController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Avo::BaseController
- Includes:
- Concerns::FiltersSessionHandler
- Defined in:
- app/controllers/avo/base_controller.rb
Direct Known Subclasses
AssociationsController, ChartsController, ResourcesController
Constant Summary
Constants included from Concerns::FindAssociationField
Concerns::FindAssociationField::ASSOCIATIONS
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #preview ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Methods included from Concerns::FiltersSessionHandler
#cache_resource_filters?, #fetch_filters, #filters_from_params, #filters_from_session, #filters_session_key, #reset_filters, #save_filters_to_session
Methods inherited from ApplicationController
#exception_logger, #turbo_frame_request?
Methods included from Concerns::FindAssociationField
Methods included from Concerns::Breadcrumbs
#add_breadcrumb, #avo_breadcrumbs
Methods included from UrlHelpers
#edit_resource_path, #new_resource_path, #preview_resource_path, #related_resources_path, #resource_attach_path, #resource_detach_path, #resource_path, #resource_view_path, #resources_path
Methods included from ApplicationHelper
#a_button, #a_link, #button_classes, #card_classes, #chart_color, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #render_license_warning, #root_path_without_url, #svg, #white_panel_classes
Methods included from ResourcesHelper
#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_selector_data_attributes, #resource_grid, #resource_show_path, #resource_table
Methods included from CommonController
#default_url_options, #extra_default_url_options
Methods included from InitializesAvo
#_current_user, #context, #init_app
Instance Method Details
#create ⇒ Object
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 166 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 137 def create # This means that the record has been created through another parent record and we need to attach it somehow. if params[:via_record_id].present? && params[:via_belongs_to_resource_class].nil? @reflection = @record.class.reflect_on_association(params[:via_relation]) # Figure out what kind of association does the record have with the parent record # Fills in the required info 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) = Avo.resource_manager.get_resource_by_model_class params[:via_relation_class] = .find_record params[:via_record_id], params: params @record.send(:"#{@reflection.foreign_key}=", .id) 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) || @reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection) # find the record via_resource = Avo.resource_manager.get_resource_by_model_class(params[:via_relation_class]) @related_record = via_resource.find_record params[:via_record_id], params: params association_name = BaseResource.valid_association_name(@record, params[:via_relation]) if params[:via_association_type] == "has_one" # On has_one scenarios we should switch the @record and @related_record @related_record.send(:"#{@reflection.parent_reflection.inverse_of.name}=", @record) else @record.send(association_name) << @related_record end end end # record gets instantiated and filled in the fill_record method saved = save_record @resource.hydrate(record: @record, view: Avo::ViewInquirer.new(:new), user: _current_user) @resource.plural_name.humanize, resources_path(resource: @resource) t("avo.new").humanize set_actions set_component_for :edit if saved create_success_action else create_fail_action end end |
#destroy ⇒ Object
206 207 208 209 210 211 212 |
# File 'app/controllers/avo/base_controller.rb', line 206 def destroy if destroy_model destroy_success_action else destroy_fail_action end end |
#edit ⇒ Object
185 186 187 188 189 |
# File 'app/controllers/avo/base_controller.rb', line 185 def edit set_actions set_component_for __method__ end |
#index ⇒ Object
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 |
# File 'app/controllers/avo/base_controller.rb', line 19 def index @page_title = @resource.plural_name.humanize if @reflection.present? && !turbo_frame_request? @record.class.to_s.pluralize, resources_path(resource: @parent_resource) @parent_resource.record_title, resource_path(record: @record, resource: @parent_resource) end @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 # Eager load the associations if @resource.includes.present? @query = @query.includes(*@resource.includes) end # Eager load attachments if @resource..present? @resource..each do || @query = @query.send(:"with_attached_#{}") end end apply_sorting # Apply filters to the current query filters_to_be_applied.each do |filter_class, filter_value| @query = filter_class.safe_constantize.new( arguments: @resource.get_filter_arguments(filter_class) ).apply_query request, @query, filter_value end safe_call :set_and_apply_scopes safe_call :apply_dynamic_filters apply_pagination # Create resources for each record # Duplicate the @resource before hydration to avoid @resource keeping last record. @resource.hydrate(params: params) @resources = @records.map do |record| @resource.dup.hydrate(record: record) end set_component_for __method__ end |
#new ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/controllers/avo/base_controller.rb', line 105 def new # Record is already hydrated on set_record_to_fill method @record = @resource.record @resource.hydrate(view: Avo::ViewInquirer.new(:new), user: _current_user) # Handle special cases when creating a new record via a belongs_to relationship if params[:via_belongs_to_resource_class].present? return render turbo_stream: turbo_stream.append(Avo::MODAL_FRAME_ID, partial: "avo/base/new_via_belongs_to") end set_actions @page_title = @resource.default_panel_name.to_s if is_associated_record? via_resource = Avo.resource_manager.get_resource_by_model_class(params[:via_relation_class]) via_record = via_resource.find_record params[:via_record_id], params: params via_resource = via_resource.new record: via_record via_resource.plural_name, resources_path(resource: via_resource) via_resource.record_title, resource_path(record: via_record, resource: via_resource) @resource.plural_name.humanize else @resource.plural_name.humanize, resources_path(resource: @resource) end t("avo.new").humanize set_component_for __method__, fallback_view: :edit end |
#preview ⇒ Object
214 215 216 217 218 |
# File 'app/controllers/avo/base_controller.rb', line 214 def preview @resource.hydrate(record: @record, view: Avo::ViewInquirer.new(:show), user: _current_user, params: params) render layout: params[:turbo_frame].blank? end |
#show ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/controllers/avo/base_controller.rb', line 72 def show @resource.hydrate( record: @record, view: Avo::ViewInquirer.new(:show), user: _current_user, params: params ).detect_fields 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_record_id].present? via_resource = Avo.resource_manager.get_resource(params[:via_resource_class]) via_record = via_resource.find_record params[:via_record_id], params: params via_resource = via_resource.new record: via_record via_resource.plural_name, resources_path(resource: via_resource) via_resource.record_title, resource_path(record: via_record, resource: via_resource) @resource.plural_name.humanize else @resource.plural_name.humanize, resources_path(resource: @resource) end @resource.record_title I18n.t("avo.details").upcase_first set_component_for __method__ end |
#update ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/controllers/avo/base_controller.rb', line 191 def update # record gets instantiated and filled in the fill_record method saved = save_record @resource = @resource.hydrate(record: @record, view: Avo::ViewInquirer.new(:edit), user: _current_user) set_actions set_component_for :edit if saved update_success_action else update_fail_action end end |