summaryrefslogtreecommitdiff
path: root/app/controllers/assets_controller.rb
blob: 4b27d1c403d00f4a73174ecb0866bf277d746bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class AssetsController < ApplicationController
  include PinnedToDefaultLocale
  
  # Private
  
  before_action :login_required
  
  layout 'admin'

  def index
    scope = params[:q].present? ? Asset.editor_search(params[:q]) : Asset.all
    @assets = scope.order("id DESC").paginate(:page => params[:page], :per_page => 20)
  end

  # GET /assets/1
  # GET /assets/1.xml
  def show
    @asset = Asset.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @asset }
    end
  end

  # GET /assets/new
  # GET /assets/new.xml
  def new
    @asset = Asset.new
    @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present?

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @asset }
    end
  end

  # GET /assets/1/edit
  def edit
    @asset = Asset.find(params[:id])
  end

  # POST /assets
  # POST /assets.xml
  def create
    @asset = Asset.new(asset_params)
    @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present?

    respond_to do |format|
      if @asset.save
        flash[:notice] = t("flash.assets.created")
        NodeAction.record!(:participants => [@asset], :user => current_user,
                            :action => "asset_create",
                            :asset_name   => @asset.name,
                            :content_type => @asset.upload_content_type,
                            :path         => @asset.upload.url.sub(/\?\d+$/, ""))
        attach_to(@attach_node) if @attach_node
        format.html { redirect_to(@asset) }
        format.xml  { render :xml => @asset, :status => :created, :location => @asset }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @asset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /assets/1
  # PUT /assets/1.xml
  def update
    @asset = Asset.find(params[:id])

    respond_to do |format|
      if @asset.update(asset_params)
        flash[:notice] = t("flash.assets.updated")
        attach_to(@attach_node) if @attach_node
        format.html { redirect_to(@asset) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @asset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /assets/1
  # DELETE /assets/1.xml
  def destroy
    @asset = Asset.find(params[:id])
    @asset.destroy_witnessed!(:user => current_user)

    respond_to do |format|
      format.html { redirect_to(assets_url) }
      format.xml  { head :ok }
    end
  rescue ActiveRecord::RecordInvalid => e
    flash[:error] = e.record.errors.full_messages.to_sentence
    respond_to do |format|
      format.html { redirect_to(asset_path(@asset)) }
      format.xml  { head :forbidden }
    end
  end

  # POST /assets/1/attach_to_node
  def attach_to_node
    @asset = Asset.find(params[:id])
    node   = Node.not_in_trash.find_by(:id => params[:node_id])

    if node
      attach_to(node)
    else
      flash[:error] = t("flash.assets.attach_no_node")
    end

    redirect_to(asset_path(@asset))
  end

  private

    def asset_params
      params.require(:asset).permit(:name, :upload, :creator, :source_url, :license_key)
    end

    def attach_to node
      result = node.attach_asset!(@asset, :user => current_user,
                                   :headline => params[:headline].present?)
      sentence =
        if result[:attached].zero?
          t("flash.assets.already_attached", :title => node.title)
        elsif result[:draft_created]
          t("flash.assets.attached_new_draft", :title => node.title)
        else
          t("flash.assets.attached_to_draft", :title => node.title)
        end
      case result[:headline]
      when :set           then sentence += " " + t("flash.common.now_headline")
      when :kept_existing then flash[:headline_kept_path] = node_path(node)
      when :not_eligible  then flash[:error] = t("flash.common.headline_ineligible")
      end
      flash[:notice] = [flash[:notice], sentence].compact.join(" ")
    rescue LockedByAnotherUser
      flash[:locked_by]        = node.lock_owner&.login
      flash[:locked_node_path] = node_path(node)
    rescue ActiveRecord::RecordInvalid => e
      flash[:error]             = e.record.errors.full_messages.to_sentence
      flash[:resolve_node_path] = node_path(node)
    end
end