Add asset model and controller
This commit is contained in:
1 parent
d5c5200b71
commit
a19e0dba6c
15 files changed
+290
-2
No files matched your search
@@ -0,0 +1,51 @@
|
||||
class Admin::AssetsController < ApplicationController
|
||||
|
||||
layout "admin"
|
||||
|
||||
def show
|
||||
@asset = Asset.find(params[:id])
|
||||
send_data @asset.data.file.read, :filename => @asset.filename,
|
||||
:type => @asset.data.content_type,
|
||||
:disposition => 'inline' # or 'attachment'
|
||||
end
|
||||
|
||||
def index
|
||||
@assets = Asset.find(:all)
|
||||
end
|
||||
|
||||
def edit
|
||||
@asset = Asset.find(params[:id])
|
||||
end
|
||||
|
||||
def new
|
||||
@asset = Asset.new
|
||||
end
|
||||
|
||||
def create
|
||||
@asset = Asset.new(params[:asset])
|
||||
if @asset.save
|
||||
redirect_to admin_assets_url
|
||||
else
|
||||
render :action => :new
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def update
|
||||
@asset = Asset.find(params[:id])
|
||||
if @asset.update_attributes(params[:asset])
|
||||
redirect_to admin_assets_url
|
||||
else
|
||||
render :action => :edit
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def destroy
|
||||
@asset = Asset.find(params[:id])
|
||||
@asset.destroy
|
||||
|
||||
redirect_to admin_assets_url
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
class Asset
|
||||
|
||||
include MongoMapper::Document
|
||||
include Grip::HasAttachment
|
||||
|
||||
key :filename, String, :required => true
|
||||
key :description, String
|
||||
|
||||
has_grid_attachment :data, :required => true
|
||||
|
||||
before_validation :setup_filename
|
||||
|
||||
protected
|
||||
|
||||
def setup_filename
|
||||
if self.filename.blank? && self.data
|
||||
self.filename = self.data.file_name
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
class AssetImage < Asset
|
||||
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
class AssetJavascript < Asset
|
||||
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
class AssetPdf < Asset
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
class AssetStylesheet < Asset
|
||||
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p>
|
||||
<%= f.label :filename, "FileName" %>
|
||||
<%= f.text_field :filename, :class => 'text' %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.label :description, "Description" %>
|
||||
<%= f.text_field :description, :class => 'text' %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.label :data, "Data" %>
|
||||
<%= f.file_field :data %>
|
||||
</p>
|
||||
@@ -0,0 +1,12 @@
|
||||
<h1>Editing assets</h1>
|
||||
|
||||
<% form_for @asset, :url => admin_asset_path(@asset), :html => { :multipart => true } do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<%= render :partial => "form", :locals => { :f => f } %>
|
||||
<p>
|
||||
<%= f.submit 'Update' %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Back', admin_assets_path %>
|
||||
@@ -0,0 +1,29 @@
|
||||
<h1>Listing assets</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>檔案</th>
|
||||
<th>描述</th>
|
||||
<th>格式</th>
|
||||
<th>原上傳檔名</th>
|
||||
<th>檔案大小</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
||||
<% @assets.each do |asset| %>
|
||||
<tr>
|
||||
<td><%= link_to asset.filename, admin_asset_path(asset) %></td>
|
||||
<td><%= asset.description %></td>
|
||||
<td><%= asset.data.content_type %></td>
|
||||
<td><%= asset.data.file_name %></td>
|
||||
<td><%= number_to_human_size(asset.data.file_size) %></td>
|
||||
<td><%= link_to t(:edit), edit_admin_asset_path(asset) %></td>
|
||||
<td><%= link_to t(:delete), admin_asset_path(asset), :confirm => 'Are you sure?', :method => :delete %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
|
||||
<%= link_to t(:new_asset, :scope => :admin), new_admin_asset_path, :class => 'button positive' %>
|
||||
@@ -0,0 +1,11 @@
|
||||
<h1>New Asset</h1>
|
||||
|
||||
<% form_for :asset, :url => admin_assets_path, :html => { :multipart => true } do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<%= render :partial => "form", :locals => { :f => f } %>
|
||||
|
||||
<p>
|
||||
<%= f.submit 'Create' %>
|
||||
</p>
|
||||
<% end %>
|
||||
@@ -22,6 +22,7 @@
|
||||
<li><%= link_to t(:home, :scope => :admin), root_path %></li>
|
||||
<li><%= link_to t(:item, :scope => :admin), admin_items_path %></li>
|
||||
<li><%= link_to t(:layout, :scope => :admin), admin_layouts_path %></li>
|
||||
<li><%= link_to t(:asset, :scope => :admin), admin_assets_path %></li>
|
||||
<li><%= link_to t(:announcement, :scope => :admin), panel_announcements_path( :entry_name => 'news' ) %></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user