Class: Alchemy::Admin::ResourcesController

Inherits:
BaseController show all
Includes:
ResourcesHelper
Defined in:
app/controllers/alchemy/admin/resources_controller.rb

Constant Summary collapse

COMMON_SEARCH_FILTER_EXCLUDES =
%i[id utf8 _method _ format].freeze

Instance Method Summary collapse

Methods included from ResourcesHelper

#contains_relations?, #edit_resource_path, #new_resource_path, #render_attribute, #render_resources, #resource_attribute_field_options, #resource_has_tags, #resource_instance_variable, #resource_model, #resource_name, #resource_path, #resource_relations_names, #resource_scope, #resource_url_proxy, #resource_window_size, #resources_instance_variable, #resources_path, #sortable_resource_header_column

Methods inherited from BaseController

#leave

Methods included from Modules

included, #module_definition_for, register_module

Methods included from Alchemy::AbilityHelper

#current_ability

Methods included from ConfigurationMethods

#configuration, #multi_language?, #multi_site?, #prefix_locale?

Instance Method Details

#createObject



56
57
58
59
60
61
62
63
64
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 56

def create
  instance_variable_set(:"@#{resource_handler.resource_name}", resource_handler.model.new(resource_params))
  resource_instance_variable.save
  render_errors_or_redirect(
    resource_instance_variable,
    resources_path(resource_instance_variable.class, search_filter_params),
    flash_notice_for_resource_action
  )
end

#destroyObject



75
76
77
78
79
80
81
82
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 75

def destroy
  resource_instance_variable.destroy
  if resource_instance_variable.errors.any?
    flash[:error] = resource_instance_variable.errors.full_messages.join(", ")
  end
  flash_notice_for_resource_action
  do_redirect_to resource_url_proxy.url_for(search_filter_params.merge(action: "index"))
end

#editObject



53
54
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 53

def edit
end

#indexObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 25

def index
  @query = resource_handler.model.ransack(search_filter_params[:q])
  @query.sorts = default_sort_order if @query.sorts.empty?
  items = @query.result

  items = items.includes(*resource_relations_names) if contains_relations?
  items = items.tagged_with(search_filter_params[:tagged_with]) if search_filter_params[:tagged_with].present?
  items = apply_filters(items) if search_filter_params[:filter].present?

  respond_to do |format|
    format.html do
      items = items.page(params[:page] || 1).per(items_per_page)
      instance_variable_set(:"@#{resource_handler.resources_name}", items)
    end
    format.csv do
      instance_variable_set(:"@#{resource_handler.resources_name}", items)
    end
  end
end

#newObject



45
46
47
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 45

def new
  instance_variable_set(:"@#{resource_handler.resource_name}", resource_handler.model.new)
end

#resource_filtersObject



96
97
98
99
100
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 96

def resource_filters
  return unless resource_has_filters

  @_resource_filters ||= resource_model.alchemy_resource_filters
end

#resource_filters_for_selectObject



102
103
104
105
106
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 102

def resource_filters_for_select
  resource_filters.map do |filter|
    ResourceFilter.new(filter, resource_handler.resource_name)
  end
end

#resource_handlerObject



84
85
86
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 84

def resource_handler
  @_resource_handler ||= Alchemy::Resource.new(controller_path, alchemy_module)
end

#resource_has_deprecated_filtersObject



92
93
94
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 92

def resource_has_deprecated_filters
  resource_model.alchemy_resource_filters.any? { |f| !f.is_a?(Hash) }
end

#resource_has_filtersObject



88
89
90
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 88

def resource_has_filters
  resource_model.respond_to?(:alchemy_resource_filters)
end

#showObject



49
50
51
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 49

def show
  render action: "edit"
end

#updateObject



66
67
68
69
70
71
72
73
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 66

def update
  resource_instance_variable.update(resource_params)
  render_errors_or_redirect(
    resource_instance_variable,
    resources_path(resource_instance_variable.class, search_filter_params),
    flash_notice_for_resource_action
  )
end