module Elabs class ElabsApplicationController < ActionController::Base include Elabs::Concerns::Reportable include Elabs::Concerns::Localizable ALLOWED_NESTED_FROM = [].freeze ALLOWED_ORDER_FROM = [].freeze DEFAULT_ORDER = { id: :desc }.freeze IS_NSFW_FILTERABLE = false MAX_ITEMS_PER_PAGE = Elabs.max_items_per_page before_action :set_nsfw_option protect_from_forgery with: :exception layout 'elabs/layouts/application' private def scope_request(orm_request) orm_request = build_request_nested orm_request orm_request = build_request_where orm_request orm_request = build_request_nsfw orm_request if self.class::IS_NSFW_FILTERABLE build_request_order orm_request end def build_request_nested(orm_request) where_clauses = {} params.each do |p| # No need to do this with "album_id" as there is no action requiring it (as for a /albums/xxx/uploads) next unless %w[tag_id project_id].include? p[0] foreign_table = p[0].chomp('_id').pluralize.to_sym orm_request = orm_request.joins(foreign_table) where_clauses[foreign_table] = { id: p[1] } next end orm_request.where(where_clauses) end def build_request_where(orm_request) allowed_scopes_ids = self.class::ALLOWED_NESTED_FROM.map { |n| "#{n}_id" } clean_list = allowed_scopes_ids.reject { |s| %w[tag_id project_id].include? s } params.each do |p| orm_request = orm_request.where(p[0] => p[1]) if clean_list.include? p[0] end orm_request end def build_request_order(orm_request) allowed_order_fields = self.class::ALLOWED_ORDER_FROM.map { |n| "order_by_#{n}" } orders = 0 params.each do |p| next unless allowed_order_fields.include? p[0] field = p[0].gsub(/^order_by_/, '') orm_request = orm_request.order "#{field}_id" => p[1] orders += 1 end orm_request = orm_request.order(self.class::DEFAULT_ORDER) if orders.zero? orm_request end def build_request_nsfw(orm_request) if params['sfw_status'] return case params['sfw_status'] when 'sfw_only' orm_request.where(sfw: true) when 'nsfw_only' orm_request.where(sfw: false) else orm_request end end orm_request end def set_nsfw_option session[:show_nsfw] ||= false session[:show_nsfw] = params['show_nsfw'] == 'true' if params['show_nsfw'] end # Wrapper method for `instance_variable_set` that adds the `@` character to the variable name. # # @return [void] def create_instance_variable(variable, entity = @entity) instance_variable_set "@#{variable}", entity end end end