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 =
[: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_filter_select, #resource_has_filters, #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



61
62
63
64
65
66
67
68
69
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 61

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



80
81
82
83
84
85
86
87
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 80

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



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

def edit; end

#indexObject



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
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 23

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

  if contains_relations?
    items = items.includes(*resource_relations_names)
  end

  if search_filter_params[:tagged_with].present?
    items = items.tagged_with(search_filter_params[:tagged_with])
  end

  if search_filter_params[:filter].present?
    items = items.public_send(sanitized_filter_params)
  end

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

#newObject



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

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

#resource_handlerObject



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

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

#showObject



55
56
57
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 55

def show
  render action: "edit"
end

#updateObject



71
72
73
74
75
76
77
78
# File 'app/controllers/alchemy/admin/resources_controller.rb', line 71

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