You are here: Home > Latest news from Darcs > Revisions inherit images from previous revision

Revision 20080729000229-9043f-fb7ba5...

Revisions inherit images from previous revision

app/models/image.rb
app/models/page.rb
app/models/revision.rb
db/migrate/029_many_images_to_many_revisions.rb
test/unit/page_test.rb

Changes to image.rb

15
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16
16
17
class Image < ActiveRecord::Base
class Image < ActiveRecord::Base
17
18
  belongs_to :revision
  has_and_belongs_to_many :revisions
18
19
19
20
  has_attachment :content_type => :image,
  has_attachment :content_type => :image,
20
21
                 :storage => :file_system,
                 :storage => :file_system,
21

Changes to page.rb

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 = revisions.last.images if revisions.last
121
121
    rev.images << image if image
    rev.images << image if image
122
122
123
123
    update_references(rev.text) if rev.text
    update_references(rev.text) if rev.text
124

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
20
  has_many :images
  has_and_belongs_to_many :images
20
21
21
22
  #Delegate methods to parent page
  #Delegate methods to parent page
22
23
  %w{name original_author revisions}.each do |method|
  %w{name original_author revisions}.each do |method|
23

Changes to 029_many_images_to_many_revisions.rb

 
class ManyImagesToManyRevisions < ActiveRecord::Migration
1
 
  def self.up
2
 
    create_table :images_revisions, :id => false do |t|
3
 
      t.column :image_id,  :integer
4
 
      t.column :revision_id,  :integer
5
 
    end
6
 
    
7
 
    execute 'INSERT INTO images_revisions (image_id, revision_id) SELECT id, revision_id FROM images WHERE revision_id is not null'
8
 
  end
9
 
10
 
  def self.down
11
 
    raise IrreversibleMigration
12
 
  end
13
 
end
14

Changes to page_test.rb

431
    assert_equal 0, page.images.size
    assert_equal 0, page.images.size
431
432
    page.revise(bob, now, {}, Image.new)
    page.revise(bob, now, {}, Image.new)
432
433
    assert_equal 1, page.images.size
    assert_equal 1, page.images.size
433
 
    page.revise(bob, now, {}, Image.new)
434
 
    assert_equal 2, page.images.size
435
434
  end
  end
436
435
437
436
private
private
438