Class: Admin::EmoticonsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/my_forum/admin/emoticons_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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

def create
  #TODO validation add

  # Create dir of not exists
  FileUtils::mkdir_p Emoticon::UPLOAD_PATH

  uploaded_io = params[:emoticon][:file_name]
  File.open(File.join(Emoticon::UPLOAD_PATH, uploaded_io.original_filename), 'wb') do |file|
    file.write(uploaded_io.read)
  end

  attachment = Emoticon.create(code: params[:emoticon][:code], file_name: uploaded_io.original_filename, is_active: true)

  redirect_to admin_emoticons_path
end

#destroyObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/my_forum/admin/emoticons_controller.rb', line 44

def destroy
  return unless emoticon = Emoticon.find(params[:id])

  begin
    FileUtils.rm File.join(Emoticon::UPLOAD_PATH, emoticon.file_name)
    emoticon.destroy
  rescue
    Rails.logger.error 'can`t find and delete emoticon file'
  ensure
    emoticon.destroy
  end

  redirect_to admin_emoticons_path
end

#editObject



33
34
35
# File 'app/controllers/my_forum/admin/emoticons_controller.rb', line 33

def edit
  @emoticon = Emoticon.find(params[:id])
end

#indexObject



9
10
11
# File 'app/controllers/my_forum/admin/emoticons_controller.rb', line 9

def index
  @emoticons = Emoticon.all
end

#newObject



13
14
15
# File 'app/controllers/my_forum/admin/emoticons_controller.rb', line 13

def new
  @emoticon = Emoticon.new
end

#updateObject



37
38
39
40
41
42
# File 'app/controllers/my_forum/admin/emoticons_controller.rb', line 37

def update
  emoticon = Emoticon.find(params[:id])
  emoticon.update(emoticon_params)

  redirect_to admin_emoticons_path
end