From cf1b60e0cfa7d1a8f4a80d686649cc12e73a634e Mon Sep 17 00:00:00 2001 From: hukl Date: Fri, 24 Apr 2009 11:43:08 +0200 Subject: Integrated basic Asset upload functionality. You can upload files now and use their url in pages. --- app/controllers/assets_controller.rb | 88 ++++++++++++++++++++++++++++++++++++ app/helpers/assets_helper.rb | 2 + app/models/asset.rb | 10 ++++ app/views/admin/_menu.html.erb | 1 + app/views/assets/edit.html.erb | 12 +++++ app/views/assets/index.html.erb | 20 ++++++++ app/views/assets/new.html.erb | 17 +++++++ app/views/assets/show.html.erb | 10 ++++ app/views/layouts/assets.html.erb | 17 +++++++ 9 files changed, 177 insertions(+) create mode 100644 app/controllers/assets_controller.rb create mode 100644 app/helpers/assets_helper.rb create mode 100644 app/models/asset.rb create mode 100644 app/views/assets/edit.html.erb create mode 100644 app/views/assets/index.html.erb create mode 100644 app/views/assets/new.html.erb create mode 100644 app/views/assets/show.html.erb create mode 100644 app/views/layouts/assets.html.erb (limited to 'app') diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb new file mode 100644 index 0000000..4734a54 --- /dev/null +++ b/app/controllers/assets_controller.rb @@ -0,0 +1,88 @@ +class AssetsController < ApplicationController + + layout 'admin' + + # GET /assets + # GET /assets.xml + def index + @assets = Asset.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @assets } + end + 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 + + 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(params[:asset]) + + respond_to do |format| + if @asset.save + flash[:notice] = 'Asset was successfully created.' + 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_attributes(params[:asset]) + flash[:notice] = 'Asset was successfully updated.' + 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 + + respond_to do |format| + format.html { redirect_to(assets_url) } + format.xml { head :ok } + end + end +end diff --git a/app/helpers/assets_helper.rb b/app/helpers/assets_helper.rb new file mode 100644 index 0000000..9824bb4 --- /dev/null +++ b/app/helpers/assets_helper.rb @@ -0,0 +1,2 @@ +module AssetsHelper +end diff --git a/app/models/asset.rb b/app/models/asset.rb new file mode 100644 index 0000000..c9c6296 --- /dev/null +++ b/app/models/asset.rb @@ -0,0 +1,10 @@ +class Asset < ActiveRecord::Base + has_attached_file( + :upload, + :styles => { + :normal => "450x450", + :medium => "300x300", + :thumb => "100x100", + } + ) +end diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 8f48822..5ae4307 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -2,4 +2,5 @@ <%= link_to 'Overview', :controller => :admin, :action => 'index' %> <%= link_to 'Nodes', nodes_path, selected?('nodes') %> +<%= link_to 'Assets', assets_path, selected?('assets') %> <%= link_to 'User', users_path, selected?('users') %> > \ No newline at end of file diff --git a/app/views/assets/edit.html.erb b/app/views/assets/edit.html.erb new file mode 100644 index 0000000..d60db94 --- /dev/null +++ b/app/views/assets/edit.html.erb @@ -0,0 +1,12 @@ +

Editing asset

+ +<% form_for(@asset) do |f| %> + <%= f.error_messages %> + +

+ <%= f.submit 'Update' %> +

+<% end %> + +<%= link_to 'Show', @asset %> | +<%= link_to 'Back', assets_path %> \ No newline at end of file diff --git a/app/views/assets/index.html.erb b/app/views/assets/index.html.erb new file mode 100644 index 0000000..c9be684 --- /dev/null +++ b/app/views/assets/index.html.erb @@ -0,0 +1,20 @@ +<% content_for :subnavigation do %> + <%= link_to 'New asset', new_asset_path %> +<% end %> + + + + + + +<% @assets.each do |asset| %> + + + + + + + + +<% end %> +
<%= image_tag asset.upload.url(:thumb) %><%= link_to asset.name, asset.upload.url %><%= asset.upload.content_type %><%= link_to 'Show', asset %><%= link_to 'Edit', edit_asset_path(asset) %><%= link_to 'Destroy', asset, :confirm => 'Are you sure?', :method => :delete %>
\ No newline at end of file diff --git a/app/views/assets/new.html.erb b/app/views/assets/new.html.erb new file mode 100644 index 0000000..366488f --- /dev/null +++ b/app/views/assets/new.html.erb @@ -0,0 +1,17 @@ +

New asset

+ +<% form_for(@asset, :html => { :multipart => true }) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :name %>
+ <%= f.text_field :name %> +

+ +

+ <%= f.file_field :upload %> + <%= f.submit 'Create' %> +

+<% end %> + +<%= link_to 'Back', assets_path %> \ No newline at end of file diff --git a/app/views/assets/show.html.erb b/app/views/assets/show.html.erb new file mode 100644 index 0000000..dd657d3 --- /dev/null +++ b/app/views/assets/show.html.erb @@ -0,0 +1,10 @@ +<% content_for :subnavigation do %> + <%= link_to 'Edit', edit_asset_path(@asset) %> + <%= link_to 'Back', assets_path %> +<% end %> + + + +<%= image_tag @asset.upload.url(:medium) %>
+<%= @asset.upload.content_type %>
+<%= "#{@asset.upload.size/1024} KB" %>
\ No newline at end of file diff --git a/app/views/layouts/assets.html.erb b/app/views/layouts/assets.html.erb new file mode 100644 index 0000000..9d629c8 --- /dev/null +++ b/app/views/layouts/assets.html.erb @@ -0,0 +1,17 @@ + + + + + + Assets: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + -- cgit v1.3