Class: Admin::AttachmentsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/admin/attachments_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /medias



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/admin/attachments_controller.rb', line 56

def create
  respond_to do |format|
    format.json do
      if params[:Filedata]
        require 'mime/types'
        filename = params[:Filename] || params[:Filedata].original_path
        @content_type = MIME::Types.type_for(filename).first.to_s
        media_class = Media
        [Audio,Video,Pdf,Doc,Picture].each do |klass|
          media_class = klass if klass.attachment_options[:content_type].include?(@content_type)
        end

        @media = media_class.new(params[:attachment])
        @media.uploaded_data = { 'tempfile' => params[:Filedata], 'content_type' => @content_type, 'filename' => Forgeos::url_generator(filename)}

        if @media.save
          flash[:notice] = I18n.t('media.create.success').capitalize

          if params[:target] && params[:target_id] && !params[:target].blank?
            type = target.constantize
            object = type.find_by_id(params[:target_id])
            attachments = (object.attachment_ids << @media.id)
            object.update_attribute('attachment_ids', attachments)
          end

          if params[:parent_id]
            parent_category = Category.find_by_id(params[:parent_id])
            if parent_category
             @media.attachment_categories << parent_category
            end
          end

          render :json => { :result => 'success', :id => @media.id, :path => @media.public_filename(''), :size => @media.size, :type => @media.type.to_s.upcase }
        else
          render :json => { :result => 'error', :error => @media.errors.first }
        end
      else
        render :json => { :result => 'error', :error => 'bad parameters' }
      end
    end
    format.js do
      if params[:Filedata]
        require 'mime/types'
        filename = params[:Filedata].original_path
        @content_type = MIME::Types.type_for(filename).first.to_s
        media_class = Media
        [Video,Pdf,Doc,Picture].each do |klass|
          media_class = klass if klass.attachment_options[:content_type].include?(@content_type)
        end

        @media = media_class.new(params[:attachment])
        @media.uploaded_data = { 'tempfile' => params[:Filedata], 'content_type' => @content_type, 'filename' => Forgeos::url_generator(filename)}

        @media.save
      end
        responds_to_parent do
          render(:update) do |page|
            page << "upload_callback();"
          end
        end
    end
  end
end

#destroyObject

DELETE /medias/1



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/admin/attachments_controller.rb', line 121

def destroy
  if @media.destroy
    flash[:notice] = I18n.t('media.destroy.success').capitalize
  else
    flash[:notice] = I18n.t('media.destroy.failed').capitalize
  end
  respond_to do |wants|
    wants.html do
      redirect_to([forgeos_core, :admin, @media.class.to_s.underscore.pluralize])
    end
    wants.js
  end
end

#downloadObject

GET /medias/1



27
28
29
# File 'app/controllers/admin/attachments_controller.rb', line 27

def download
  send_file(@media.full_filename)
end

#editObject

GET /medias/1/edit



32
33
# File 'app/controllers/admin/attachments_controller.rb', line 32

def edit
end

#indexObject



12
13
14
15
16
17
18
19
20
# File 'app/controllers/admin/attachments_controller.rb', line 12

def index
  respond_to do |format|
    format.html
    format.json do
      sort
      render :layout => false
    end
  end
end

#manageObject



7
8
9
10
# File 'app/controllers/admin/attachments_controller.rb', line 7

def manage
  @attachments = "#{params[:file_type]}".classify.constantize.paginate :page => params[:page], :order => 'created_at DESC', :per_page => 10, :conditions => { :parent_id => nil}
  render :partial => 'tiny_mce_list'
end

#showObject

GET /medias/1



23
24
# File 'app/controllers/admin/attachments_controller.rb', line 23

def show
end

#updateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/admin/attachments_controller.rb', line 35

def update
  file_type = nil
  %w(media picture pdf audio video doc).each do |key|
    if params[key]
      params[:attachment] = params[key]
      file_type = key
    end
  end
  params[:attachment][:filename] = @media.filename
  rack_file = params[:attachment][:uploaded_data]
  if !(rack_file.respond_to?(:file_type) && !@media.class.attachment_options[:content_type].include?(rack_file.file_type)) &&
    @media.update_attributes(params[:attachment])
    flash[:notice] = I18n.t('media.update.success').capitalize
  else
    flash[:error] = I18n.t('product.update.failed').capitalize
    edit
  end
  render :action => 'edit'
end