Class: Assetable::AssetsController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/assetable/assets_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Create a new asset



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/assetable/assets_controller.rb', line 11

def create
  # Get the content type
  content_type = params[:file].content_type
  asset_params = {name: params[:file].original_filename, filename: params[:file]}

  # Create the appropriate model
  if content_type.split("/").first == "image"
    @asset = Image.new(asset_params)
  elsif content_type.split("/").first == "video"
    @asset = Video.new(asset_params)
  elsif content_type.split("/").first == "application"
    @asset = Document.new(asset_params)
  end

  # Return
  if @asset.errors.empty? and @asset.save
    render json: { success: true, html: render_to_string(partial: "assetable/assets/asset", locals: { asset: @asset, fieldname: params[:fieldname]})}
  else
    render json: { status: "error", errors: @asset.errors }
  end
end

#editObject



33
34
35
# File 'app/controllers/assetable/assets_controller.rb', line 33

def edit
  @asset = Asset.find(params[:id])
end

#indexObject



5
6
7
8
# File 'app/controllers/assetable/assets_controller.rb', line 5

def index
  @assets = Asset.page(params[:page]).per(20)
  render json: { success: true, html: render_to_string(partial: "assetable/assets/gallery", locals: {assets: @assets, fieldname: params[:fieldname]})}
end

#permitted_paramsObject

Permitted params for the model



47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/assetable/assets_controller.rb', line 47

def permitted_params
  params.require(params[:asset_type].underscore.to_sym).permit(
    :name,
    :filename,
    :body,
    :content_type,
    :width,
    :height,
    :asset_type
  )
end

#updateObject



37
38
39
40
41
42
43
44
# File 'app/controllers/assetable/assets_controller.rb', line 37

def update
  @asset = Asset.find(params[:id])
  if @asset and @asset.errors.empty? and @asset.update_attributes(permitted_params)
    render json: { success: true, id: @asset.id, html: render_to_string(partial: "assetable/assets/asset", locals: { asset: @asset, fieldname: params[:fieldname]})}
  else
    render json: { status: "error", errors: @external_service.errors.full_messages, html: render_to_string(:edit) }
  end
end