Class: RailsAdminImageManager::ImagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_admin_image_manager/images_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#_current_user

Instance Method Details

#createObject



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

def create
  _check_permissions(:create)

  my_params = images_params

  # Handling dynamic tag creating when receiving a string
  tags = []
  images_params[:tags].each do |tag_string|
   tags << RailsAdminImageManager::Tag.retrieve_or_create_tag(tag_string)
  end
  my_params[:tags] = tags

  image = RailsAdminImageManager::File.new(my_params)
  if image.save()
    render json: image, status: :ok
  else
    render json: image.errors, status: :unprocessable_entity
  end
end

#destroyObject



95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/rails_admin_image_manager/images_controller.rb', line 95

def destroy
  _check_permissions(:destroy)

  image = RailsAdminImageManager::File.find(params[:id])
  if image.destroy
    render json: image, status: :ok
  else
    render json: image.errors, status: :unprocessable_entity
  end
end

#indexObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/rails_admin_image_manager/images_controller.rb', line 4

def index
  respond_to do |format|
    format.html {
      @tags   = RailsAdminImageManager::Tag.with_files
    }

    format.json {
      images  = RailsAdminImageManager::File.select(:id, :name, :image_file_name, :created_at).order(:name).page(params[:page])
      images  = images.filter_by_text(params[:search]) if filter_by?(:search)
      images  = images.filter_by_tags(params[:tags].map{|i| i.to_i }) if filter_by?(:tags)
      images  = images.order_by_date(params[:date]) if filter_by?(:date)
      images  = images.order_by_title(params[:title]) if filter_by?(:title)

      images.each do |image|
        image.src = image.image.url(:index)
      end

      data    = { items: images, total_count: images.total_count, limit_value: images.limit_value, readonly: readonly? }

      render json: data, methods: [:src, :tags_list], status: :ok
    }
  end
end

#newObject



37
38
39
40
# File 'app/controllers/rails_admin_image_manager/images_controller.rb', line 37

def new
  _check_permissions(:create)
  render json: {msg: 'ok'}, status: :ok
end

#showObject



42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/rails_admin_image_manager/images_controller.rb', line 42

def show
  _check_permissions(:update)

  image                 = RailsAdminImageManager::File.select(:id, :name, :description, :copyright, :image_file_name).find_by!(id: params[:id])
  image.src             = image.image.url(:show)
  image.src_for_wysiwyg = {width: params[:width], height: params[:height]} if filter_by?(:width) && filter_by?(:height)

  data                  = { image: image, readonly: readonly? }

  render json: data, methods: [:src, :src_for_wysiwyg, :tags_list], status: :ok
end

#tagsObject



28
29
30
31
32
33
34
35
# File 'app/controllers/rails_admin_image_manager/images_controller.rb', line 28

def tags
  respond_to do |format|
    format.json {
      tags = RailsAdminImageManager::Tag.with_files
      render json: tags, status: :ok
    }
  end
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/rails_admin_image_manager/images_controller.rb', line 54

def update
  _check_permissions(:update)

  my_params = images_params
  image = RailsAdminImageManager::File.find(my_params[:id])

  # Handling dynamic tag creating when receiving a string
  tags = []
  images_params[:tags].each do |tag_string|
   tags << RailsAdminImageManager::Tag.retrieve_or_create_tag(tag_string)
  end
  my_params[:tags] = tags

  # Updating image object
  if image.present? && image.update_attributes(my_params)
    render json: image, status: :ok
  else
    render json: image.errors, status: :unprocessable_entity
  end
end