Class: Alchemy::Admin::PicturesController

Inherits:
ResourcesController show all
Includes:
ArchiveOverlay, UploaderResponses
Defined in:
app/controllers/alchemy/admin/pictures_controller.rb

Constant Summary

Constants inherited from ResourcesController

ResourcesController::COMMON_SEARCH_FILTER_EXCLUDES

Instance Method Summary collapse

Methods included from UploaderResponses

#failed_uploader_response, #successful_uploader_response

Methods inherited from ResourcesController

#edit, #new, #resource_handler

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



39
40
41
42
43
44
45
46
47
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 39

def create
  @picture = Picture.new(picture_params)
  @picture.name = @picture.humanized_name
  if @picture.save
    render successful_uploader_response(file: @picture)
  else
    render failed_uploader_response(file: @picture)
  end
end

#delete_multipleObject



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
104
105
106
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 78

def delete_multiple
  if request.delete? && params[:picture_ids].present?
    pictures = Picture.find(params[:picture_ids])
    names = []
    not_deletable = []
    pictures.each do |picture|
      if picture.deletable?
        names << picture.name
        picture.destroy
      else
        not_deletable << picture.name
      end
    end
    if not_deletable.any?
      flash[:warn] = Alchemy.t(
        "These pictures could not be deleted, because they were in use",
        names: not_deletable.to_sentence,
      )
    else
      flash[:notice] = Alchemy.t("Pictures deleted successfully", names: names.to_sentence)
    end
  else
    flash[:warn] = Alchemy.t("Could not delete Pictures")
  end
rescue StandardError => e
  flash[:error] = e.message
ensure
  redirect_to_index
end

#destroyObject



108
109
110
111
112
113
114
115
116
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 108

def destroy
  name = @picture.name
  @picture.destroy
  flash[:notice] = Alchemy.t("Picture deleted successfully", name: name)
rescue StandardError => e
  flash[:error] = e.message
ensure
  redirect_to_index
end

#edit_multipleObject



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

def edit_multiple
  @pictures = Picture.where(id: params[:picture_ids])
  @tags = @pictures.collect(&:tag_list).flatten.uniq.join(", ")
end

#indexObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 18

def index
  @query = Picture.ransack(search_filter_params[:q])
  @pictures = Picture.search_by(
    search_filter_params,
    @query,
    items_per_page,
  )
  @pictures = @pictures.includes(:thumbs)

  if in_overlay?
    archive_overlay
  end
end

#items_per_pageObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 118

def items_per_page
  if in_overlay?
    case @size
    when "small" then 25
    when "large" then 4
    else
      9
    end
  else
    cookies[:alchemy_pictures_per_page] = params[:per_page] ||
      cookies[:alchemy_pictures_per_page] ||
      pictures_per_page_for_size
  end
end

#items_per_page_optionsObject



133
134
135
136
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 133

def items_per_page_options
  per_page = pictures_per_page_for_size
  [per_page, per_page * 2, per_page * 4]
end

#showObject



32
33
34
35
36
37
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 32

def show
  @previous = @picture.previous(params)
  @next = @picture.next(params)
  @assignments = @picture.essence_pictures.joins(content: {element: :page})
  render action: "show"
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 54

def update
  if @picture.update(picture_params)
    @message = {
      body: Alchemy.t(:picture_updated_successfully, name: @picture.name),
      type: "notice",
    }
  else
    @message = {
      body: Alchemy.t(:picture_update_failed),
      type: "error",
    }
  end
  render :update
end

#update_multipleObject



69
70
71
72
73
74
75
76
# File 'app/controllers/alchemy/admin/pictures_controller.rb', line 69

def update_multiple
  @pictures = Picture.find(params[:picture_ids])
  @pictures.each do |picture|
    picture.update_name_and_tag_list!(params)
  end
  flash[:notice] = Alchemy.t("Pictures updated successfully")
  redirect_to_index
end