You are here: Home > Latest news from Darcs > Allow uploading of images

Revision 20080728225853-9043f-d563c0...

Allow uploading of images

app/controllers/wiki_controller.rb
app/models/image.rb
app/models/page.rb
app/models/revision.rb
app/views/wiki/_attached_images.rhtml
app/views/wiki/edit.rhtml
app/views/wiki/show.rhtml
config/environment.rb
db/migrate/028_create_images.rb
lib/kernel_extensions.rb
test/unit/page_test.rb

Changes to wiki_controller.rb

123
private
private
123
124
124
125
  def really_save
  def really_save
125
126
    @page.revise(current_user, Time.now, params[:page])
    image = Image.new(params[:image]) if try {params[:image][:uploaded_data].path}
126
 
    @page.revise(current_user, Time.now, params[:page], image)
127
127
128
128
    if 'MainPage' == @page.name
    if 'MainPage' == @page.name
129
129
      redirect_to :controller => 'root', :action => 'index'
      redirect_to :controller => 'root', :action => 'index'
130

Changes to image.rb

 
#  Motiro - A project tracking tool
1
 
#  Copyright (C) 2006-2008  Thiago Arrais
2
 
#  
3
 
#  This program is free software; you can redistribute it and/or modify
4
 
#  it under the terms of the GNU Affero General Public License as published by
5
 
#  the Free Software Foundation; either version 3 of the License, or
6
 
#  any later version.
7
 
#  
8
 
#  This program is distributed in the hope that it will be useful,
9
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
#  GNU Affero General Public License for more details.
12
 
#  
13
 
#  You should have received a copy of the GNU Affero General Public License
14
 
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
 
class Image < ActiveRecord::Base
17
 
  belongs_to :revision
18
 
19
 
  has_attachment :content_type => :image,
20
 
                 :storage => :file_system,
21
 
                 :path_prefix => 'public/wikimages',
22
 
                 :max_size => 240.kilobytes,
23
 
                 :thumbnails => { :thumb => '84x84>' }
24
 
25
 
  validates_as_attachment
26
 
27
 
end
28

Changes to page.rb

35
    oldest(:last_editor)
    oldest(:last_editor)
35
36
  end
  end
36
37
  
  
37
38
  %w{modified_at last_editor done done?}.each do |attr|
  %w{modified_at last_editor done images done?}.each do |attr|
38
39
    define_method attr do
    define_method attr do
39
40
      most_recent(attr)
      most_recent(attr)
40
41
    end
    end
41
54 more lines
96
    0 == editors.strip.size
    0 == editors.strip.size
96
97
  end
  end
97
98
  
  
98
99
  def revise(author, time, attrs)
  def revise(author, time, attrs, image = nil)
99
100
    #TODO ugly! ugly! ugly!
    #TODO ugly! ugly! ugly!
100
101
    if !attrs[:happens_at] && attrs['happens_at(1i)'] && attrs['happens_at(2i)'] && attrs['happens_at(3i)'] 
    if !attrs[:happens_at] && attrs['happens_at(1i)'] && attrs['happens_at(2i)'] && attrs['happens_at(3i)'] 
101
102
      attrs[:happens_at] =
      attrs[:happens_at] =
102
15 more lines
118
    self.happens_at = attrs[:happens_at] if attrs[:happens_at]
    self.happens_at = attrs[:happens_at] if attrs[:happens_at]
118
119
    rev.kind, rev.happens_at = self.kind, self.happens_at
    rev.kind, rev.happens_at = self.kind, self.happens_at
119
120
    rev.title, rev.text, rev.done = attrs[:title], attrs[:text], attrs[:done]
    rev.title, rev.text, rev.done = attrs[:title], attrs[:text], attrs[:done]
120
 
    rev.images << image if image
121
 
122
121
    update_references(rev.text) if rev.text
    update_references(rev.text) if rev.text
123
122
    self.revisions << rev
    self.revisions << rev
124
123
    
    
125

Changes to revision.rb

17
class Revision < ActiveRecord::Base
class Revision < ActiveRecord::Base
17
18
  belongs_to :page
  belongs_to :page
18
19
  belongs_to :last_editor, :class_name => 'User', :foreign_key => 'last_editor_id'
  belongs_to :last_editor, :class_name => 'User', :foreign_key => 'last_editor_id'
19
 
  has_many :images
20
20
21
21
  #Delegate methods to parent page
  #Delegate methods to parent page
22
22
  %w{name original_author revisions}.each do |method|
  %w{name original_author revisions}.each do |method|
23

Changes to _attached_images.rhtml

 
<div id="attachments">
1
 
  <% @page.images.each do |img| %>
2
 
    <%= link_to(image_tag(img.public_filename(:thumb)),
3
 
                img.public_filename) %>
4
 
  <% end %>
5
 
</div>
6
 
7

Changes to edit.rhtml

1
    <% form_tag :action => 'save' do %>
    <% form_tag({:action => 'save'}, {:multipart => true}) do %>
1
2
      <div id="editor" class="window">
      <div id="editor" class="window">
2
3
        <div class = "inner">
        <div class = "inner">
3
4
          <%= hidden_field_tag('page_name', params[:page_name]) if params[:page_name] %>
          <%= hidden_field_tag('page_name', params[:page_name]) if params[:page_name] %>
4
5
          <%= text_field 'page', 'title', :id => 'txtTitle' %>
          <%= text_field 'page', 'title', :id => 'txtTitle' %>
5
6
          <%= text_area 'page', 'text', :id => 'txaEditor', :rows => '30' %>
          <%= text_area 'page', 'text', :id => 'txaEditor', :rows => '30' %>
6
 
          <label for="image">Attach an image: </label>
7
 
          <%= file_field 'image', :uploaded_data %>
8
 
          <br />
9
7
          <%= submit_tag 'Save modifications'.t, :class => 'button',
          <%= submit_tag 'Save modifications'.t, :class => 'button',
10
8
                                                 :name => 'btnSave',
                                                 :name => 'btnSave',
11
9
                                                 :id => 'btnSave' %>
                                                 :id => 'btnSave' %>
12
43 more lines
53
                                 :with => 'kind',
                                 :with => 'kind',
56
54
                                 :update => 'extra_properties',
                                 :update => 'extra_properties',
57
55
                                 :loading => "extraPropertiesLoading()",
                                 :loading => "extraPropertiesLoading()",
58
56
                                 :loaded => "extraPropertiesLoaded()") %>      
                                 :loaded => "extraPropertiesLoaded()") %>      
59

Changes to show.rhtml

4
           :revision => @page_revision_id) do %>
           :revision => @page_revision_id) do %>
4
5
    <%= render :partial => 'properties_show' %>
    <%= render :partial => 'properties_show' %>
5
6
    <%= render_wiki(@page.text) %>
    <%= render_wiki(@page.text) %>
6
 
    <%= render(:partial => 'attached_images') if try {!@page.images.empty?} %>
7
7
  <% end %>
  <% end %>
8
8
  <%= render :partial => 'editbar' %>
  <%= render :partial => 'editbar' %>
9
9
<% end %>
<% end %>
10

Changes to environment.rb

9
9
10
# Bootstrap the Rails environment, frameworks, and default configuration
# Bootstrap the Rails environment, frameworks, and default configuration
10
11
require File.join(File.dirname(__FILE__), 'boot')
require File.join(File.dirname(__FILE__), 'boot')
11
 
require 'kernel_extensions'
12
12
13
13
TEMP_DIR = File.expand_path(RAILS_ROOT + '/tmp')
TEMP_DIR = File.expand_path(RAILS_ROOT + '/tmp')
14
14
15

Changes to 028_create_images.rb

 
class CreateImages < ActiveRecord::Migration
1
 
  def self.up
2
 
    create_table :images do |t|
3
 
      t.column :revision_id,  :integer
4
 
5
 
      t.column :parent_id,  :integer
6
 
      t.column :content_type, :string
7
 
      t.column :filename, :string    
8
 
      t.column :thumbnail, :string 
9
 
      t.column :size, :integer
10
 
      t.column :width, :integer
11
 
      t.column :height, :integer
12
 
    end
13
 
  end
14
 
15
 
  def self.down
16
 
    drop_table :images
17
 
  end
18
 
end
19

Changes to kernel_extensions.rb

 
module Kernel
1
 
  def try
2
 
    yield
3
 
  rescue NoMethodError
4
 
    nil
5
 
  end
6
 
end
7
 
8

Changes to page_test.rb

425
    assert bobs_page.editable_by?(bob)
    assert bobs_page.editable_by?(bob)
425
426
  end
  end
426
427
427
 
  def test_adds_images_to_page
428
 
    page = create_page_with_one_revision
429
 
430
 
    assert_equal 0, page.images.size
431
 
    page.revise(bob, now, {}, Image.new)
432
 
    assert_equal 1, page.images.size
433
 
  end
434
 
435
428
private
private
436
429
  
  
437
430
  def create_page_with_one_revision
  def create_page_with_one_revision
438