Class: Jav::BaseController

Inherits:
ApplicationController show all
Includes:
Concerns::FiltersSessionHandler
Defined in:
app/controllers/jav/base_controller.rb

Direct Known Subclasses

AssociationsController, ResourcesController

Instance Method Summary collapse

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

#_current_user, #check_jav_license, #context, #exception_logger, #init_app

Methods included from Concerns::Breadcrumbs

#add_breadcrumb, #jav_breadcrumbs

Methods included from UrlHelpers

#edit_resource_path, #new_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, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #render_license_warning, #render_license_warnings, #root_path_without_url, #svg, #white_panel_classes

Instance Method Details

#createObject



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
166
167
168
169
# File 'app/controllers/jav/base_controller.rb', line 124

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? && params[:via_belongs_to_resource_class].nil?
    @reflection = @model.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 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)
      related_resource = Jav::App.get_resource_by_model_name params[:via_relation_class]
      related_record = related_resource.find_record params[:via_resource_id], params: params

      @model.send(:"#{@reflection.foreign_key}=", related_record.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) || @reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
      # find the record
      via_resource = ::Jav::App.get_resource_by_model_name(params[:via_relation_class]).dup
      @related_record = via_resource.find_record params[:via_resource_id], params: params
      association_name = BaseResource.valid_association_name(@model, 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}=", @model)
      else
        @model.send(association_name) << @related_record
      end
    end
  end

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

  if saved
    create_success_action
  else
    create_fail_action
  end
end

#destroyObject



184
185
186
187
188
189
190
# File 'app/controllers/jav/base_controller.rb', line 184

def destroy
  if destroy_model
    destroy_success_action
  else
    destroy_fail_action
  end
end

#editObject



120
121
122
# File 'app/controllers/jav/base_controller.rb', line 120

def edit
  set_actions
end

#indexObject



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

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
  @query = @resource.class.query_scope unless defined? @query

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

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

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

  # Sort the items
  if @index_params[:sort_by].present?
    @query = @query.unscope(:order) unless @index_params[:sort_by].eql? :created_at

    sanitized_sort_direction = @index_params[:sort_direction].presence_in(%w[asc desc])

    # Check if the sortable field option is actually a proc and we need to do a custom sort
    sort_by = @index_params[:sort_by].to_sym
    field = @resource.get_field(sort_by)
    @query = if field&.sortable.is_a?(Proc)
               field.sortable.call(@query, sanitized_sort_direction)
             elsif field.present? && sanitized_sort_direction
               @query.order("#{@resource.model_class.table_name}.#{sort_by} #{sanitized_sort_direction}")
             # Transform Model to ActiveRecord::Relation because Jav expects one.
             else
               @query.where("1=1")
             end
  end

  # 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

  apply_pagination

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

#newObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/jav/base_controller.rb', line 96

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

  # Handle special cases when creating a new record via a belongs_to relationship
  return render turbo_stream: turbo_stream.append('attach_modal', partial: 'jav/base/new_via_belongs_to') if params[:via_belongs_to_resource_class].present?

  set_actions

  @page_title = @resource.default_panel_name.to_s

  if is_associated_record?
    via_resource = Jav::App.get_resource_by_model_name(params[:via_relation_class]).dup
    via_model = via_resource.find_record params[:via_resource_id], params: params
    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("jav.new").humanize
end

#orderObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/jav/base_controller.rb', line 192

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/jav/base_controller.rb', line 73

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 = Jav::App.get_resource(params[:via_resource_class]).dup
    via_model = via_resource.find_record params[:via_resource_id], params: params
    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("jav.details").upcase_first
end

#updateObject



171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/jav/base_controller.rb', line 171

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