jastix (owner)

Fork Of

Revisions

gist: 102001 Download_button fork
public
Public Clone URL: git://gist.github.com/102001.git
Embed All Files: show embed
README.txt #
1
2
3
4
5
6
7
8
9
10
11
12
==README==
 
This is a gist of Advanced Rails Recipe #13, but with Paperclip and
Paperclip_polymorph plugins. You need the following plugins for this to work:
 
  http://github.com/thoughtbot/paperclip/tree
  http://github.com/heavysixer/paperclippolymorph/tree/master
 
It allows you to attach multiple files in one form, pretty sweet. Big ups to
http://gist.github.com/patrickberkeley for his excellent port of this recipe
to Paperclip.
 
controllers/pages_controller.rb #
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
class PagesController < ApplicationController
  before_filter :login_required, :except => [ :show ]
  
  # GET /pages
  # GET /pages.xml
  def index
    @pages = Page.find(:all)
 
    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @pages }
    end
  end
 
  # GET /pages/1
  # GET /pages/1.xml
  def show
    if params[:permalink]
      @page = Page.find_by_permalink(params[:permalink])
      raise ActiveRecord::RecordNotFound, "Page not found." if @page.nil?
      respond_to do |format|
        format.html # show.html.erb
        format.xml { render :xml => @page }
      end
    else
      @page = Page.find(params[:id])
      respond_to do |format|
        format.html # show.html.erb
        format.xml { render :xml => @page }
      end
    end
  end
 
  # GET /pages/new
  # GET /pages/new.xml
  def new
    @page = Page.new
    @page.assets.build
 
    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @page }
    end
  end
 
  # GET /pages/1/edit
  def edit
    @page = Page.find(params[:id])
  end
 
  # POST /pages
  # POST /pages.xml
  def create
    @page = Page.new(params[:page])
 
    respond_to do |format|
      if @page.save
        flash[:notice] = 'Page was successfully created.'
        format.html { redirect_to(@page) }
        format.xml { render :xml => @page, :status => :created, :location => @page }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
      end
    end
  end
 
  # PUT /pages/1
  # PUT /pages/1.xml
  def update
    params[:page][:existing_asset_attributes] ||= {}
    
    @page = Page.find(params[:id])
 
    respond_to do |format|
      if @page.update_attributes(params[:page])
        flash[:notice] = 'Page was successfully updated.'
        format.html { redirect_to(@page) }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
      end
    end
  end
 
  # DELETE /pages/1
  # DELETE /pages/1.xml
  def destroy
    @page = Page.find(params[:id])
    @page.destroy
 
    respond_to do |format|
      format.html { redirect_to(pages_url) }
      format.xml { head :ok }
    end
  end
end
 
helpers/pages_helper.erb #
1
2
3
4
5
def add_asset_link(name)
    link_to_function name do |page|
      page.insert_html :bottom, :assets, :partial => 'asset', :object => Asset.new
    end
  end
models/asset.rb #
1
2
3
4
5
class Asset < ActiveRecord::Base
  belongs_to :page
  
  has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
models/page.rb #
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
class Page < ActiveRecord::Base
  acts_as_polymorphic_paperclip
  
  validates_associated :assets
  validates_presence_of :name, :permalink
  
  after_update :save_assets
  
  def new_asset_attributes=(asset_attributes)
    asset_attributes.each do |attributes|
      assets.build(attributes)
    end
  end
  
  def existing_asset_attributes=(asset_attributes)
    assets.reject(&:new_record?).each do |asset|
      attributes = asset_attributes[asset.id.to_s]
      if attributes
        asset.attributes = attributes
      else
        asset.delete(asset)
      end
    end
  end
  
  def save_assets
    assets.each do |asset|
      asset.save(false)
    end
  end
  
end
 
models/page.rb~ #
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
class Page < ActiveRecord::Base
  has_many :assets, :dependent => :destroy
  
  validates_associated :assets
  validates_presence_of :name, :permalink
  
  after_update :save_assets
  
  def new_asset_attributes=(asset_attributes)
    asset_attributes.each do |attributes|
      assets.build(attributes)
    end
  end
  
  def existing_asset_attributes=(asset_attributes)
    assets.reject(&:new_record?).each do |asset|
      attributes = asset_attributes[asset.id.to_s]
      if attributes
        asset.attributes = attributes
      else
        asset.delete(asset)
      end
    end
  end
  
  def save_assets
    assets.each do |asset|
      asset.save(false)
    end
  end
  
end
 
schema.rb #
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
  create_table "assets", :force => true do |t|
    t.string "data_file_name"
    t.string "data_content_type"
    t.integer "data_file_size"
    t.integer "attachings_count", :default => 0
    t.datetime "created_at"
    t.datetime "updated_at"
  end
 
  create_table "attachings", :force => true do |t|
    t.integer "attachable_id"
    t.integer "asset_id"
    t.string "attachable_type"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
 
  add_index "attachings", ["asset_id"], :name => "index_attachings_on_asset_id"
  add_index "attachings", ["attachable_id"], :name => "index_attachings_on_attachable_id"
 
 
  create_table "pages", :force => true do |t|
    t.string "name"
    t.string "permalink"
    t.text "content"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
 
schema.rb~ #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  create_table "assets", :force => true do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string "file_file_name"
    t.string "file_content_type"
    t.integer "file_file_size"
    t.datetime "file_updated_at"
    t.integer "page_id"
  end
 
  create_table "pages", :force => true do |t|
    t.string "name"
    t.string "permalink"
    t.text "content"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
views/pages/_asset.html.erb #
1
2
3
4
5
6
7
8
9
10
11
12
<div class="asset">
  <% new_or_existing = asset.new_record? ? 'new' : 'existing' %>
  <% prefix = "page[#{new_or_existing}_asset_attributes][]" %>
  
  <% fields_for prefix, asset do |asset_form| -%>
    <p>
      Asset: <%= asset_form.file_field :data %>
      <%= link_to_function "remove", "$(this).up('.asset').remove()" %>
    </p>
<% end -%>
</div>
 
views/pages/_asset.html.erb~ #
1
2
3
4
5
6
7
8
9
10
11
<div class="asset">
  <% new_or_existing = asset.new_record? ? 'new' : 'existing' %>
  <% prefix = "page[#{new_or_existing}_asset_attributes][]" %>
  
  <% fields_for prefix, asset do |asset_form| -%>
    <p>
      Asset: <%= asset_form.file_field :file %>
      <%= link_to_function "remove", "$(this).up('.asset').remove()" %>
    </p>
<% end -%>
</div>
views/pages/_form.html.erb #
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
<%= error_messages_for :page %>
 
<% form_for @page, :html => { :multipart => true } do |f| %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :permalink %><br />
    <%= f.text_field :permalink %>
  </p>
  <p>
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </p>
  <div id="assets">
    Attach a file or image<br />
    <%= render :partial => 'asset', :object => Asset.new %>
  </div>
  <p>
    <%= add_asset_link "Add another file" %>
  </p>
  <p>
    <%= f.submit button_name %>
  </p>
<% end %>
 
<%= link_to 'Cancel', pages_path %>
 
views/pages/_form.html.erb~ #
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
<%= error_messages_for :page %>
 
<% form_for @page, :html => { :multipart => true } do |f| %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :permalink %><br />
    <%= f.text_field :permalink %>
  </p>
  <p>
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </p>
  <div id="assets">
    Attach a file or image<br />
    <%= render :partial => 'asset', :collection => @page.assets %>
  </div>
  <p>
    <%= add_asset_link "Add a file" %>
  </p>
  <p>
    <%= f.submit button_name %>
  </p>
<% end %>
 
<%= link_to 'Cancel', pages_path %>
views/pages/index.html.erb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div class="side-nav actions">
  <%= link_to "New", new_page_path if logged_in? %>
</div>
 
<%= title('Pages', :h1)%>
 
<div id="pages">
<% for page in @pages %>
  <div class="page">
<%= render :partial => 'shared/side_nav', :locals => { :obj => page, :edit_obj => edit_page_path(page) } %>
    <div class="main">
      <h2><%= page.name %></h2>
      <div class="content"><%= truncate(page.content) %></div>
    </div>
  </div>
<% end %>
</div>
views/pages/new.html.erb and views/pages/edit.html.erb #
1
2
3
4
5
6
7
8
9
<!-- new.html.erb -->
<h1>New page</h1>
 
<%= render :partial => 'form', :locals => { :page => @page, :button_name => 'Create' } %>
 
<!-- edit.html.erb -->
<h1>Editing page</h1>
 
<%= render :partial => 'form', :locals => { :page => @page, :button_name => 'Update' } %>
views/pages/show.html.erb #
1
2
3
4
5
6
7
8
9
10
<%= render :partial => 'shared/show_side_nav', :locals => { :obj => @event, :edit_obj => edit_page_path(@page) } %>
 
<%= title(@page.name, :h1) %>
 
<%= simple_format h(@page.content) %>
 
<% for asset in @page.assets %>
  <p><%= image_tag asset.file.url %></p>
  <p>Small:<%= image_tag asset.file.url(:medium) %></p>
<% end %>