Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move email templates into a single table inheritance of messages... o…

…ne less table to play with
  • Loading branch information...
commit d917e386b9781ac31cd39aa44a9ec1ae7d3c1775 1 parent 8a9647b
@mikel authored
View
3  app/models/attachment.rb
@@ -3,11 +3,12 @@ class Attachment < ActiveRecord::Base
require 'mime/types'
belongs_to :message
+ belongs_to :email_template, :foreign_key => "message_id"
before_validation :guess_content_type
def guess_content_type
- self.content_type = MIME::Types.type_for(filename).first.content_type
+ self.content_type ||= MIME::Types.type_for(filename).first.content_type
end
def relative_path
View
5 app/models/email_template.rb
@@ -1,6 +1,3 @@
-class EmailTemplate < ActiveRecord::Base
+class EmailTemplate < Message
- belongs_to :user
- validates_presence_of :title
-
end
View
3  app/models/message.rb
@@ -16,6 +16,9 @@ def next_step
when 'template_selected'
self.html_part = email_template.html_part
self.plain_part = email_template.plain_part
+ email_template.attachments.each do |attachment|
+ self.attachments.create!(attachment.attributes)
+ end
'edit_content'
when 'file_uploaded'
self.save
View
14 db/migrate/20090518111443_create_email_templates.rb
@@ -1,14 +0,0 @@
-class CreateEmailTemplates < ActiveRecord::Migration
- def self.up
- create_table :email_templates do |t|
- t.string :title
- t.text :body
- t.integer :user_id
- t.timestamps
- end
- end
-
- def self.down
- drop_table :email_templates
- end
-end
View
1  db/migrate/20090530112907_create_messages.rb
@@ -10,6 +10,7 @@ def self.up
t.text :html_part
t.text :plain_part
+ t.string :type
t.timestamps
end
end
View
13 db/migrate/20090531073442_add_parts_to_email_template.rb
@@ -1,13 +0,0 @@
-class AddPartsToEmailTemplate < ActiveRecord::Migration
- def self.up
- add_column :email_templates, :html_part, :text
- add_column :email_templates, :plain_part, :text
- remove_column :email_templates, :body
- end
-
- def self.down
- add_column :email_templates, :body, :text
- remove_column :email_templates, :plain_part
- remove_column :email_templates, :html_part
- end
-end
View
10 db/schema.rb
@@ -25,15 +25,6 @@
t.datetime "updated_at"
end
- create_table "email_templates", :force => true do |t|
- t.string "title"
- t.integer "user_id"
- t.datetime "created_at"
- t.datetime "updated_at"
- t.text "html_part"
- t.text "plain_part"
- end
-
create_table "memberships", :force => true do |t|
t.integer "user_id"
t.integer "role_id"
@@ -50,6 +41,7 @@
t.integer "email_template_id"
t.text "html_part"
t.text "plain_part"
+ t.string "type"
t.datetime "created_at"
t.datetime "updated_at"
end
View
8 features/step_definitions/manage_email_templates_steps.rb
@@ -7,5 +7,11 @@
Given /^the email template entitled "([^\"]*)" has an image attached called "([^\"]*)"$/ do |title, image_name|
template = EmailTemplate.find_by_title(title)
- attachment = Factory(:attachment, :data => File.read(File.join(RAILS_ROOT, 'spec', 'resources', 'rails.png')))
+ data = File.read(File.join(RAILS_ROOT, 'spec', 'resources', image_name))
+ attachment = Factory(:attachment,
+ :filename => 'rails.png',
+ :directory => 'images',
+ :data => data,
+ :content_type => 'image/png',
+ :message_id => template.id)
end
View
17 spec/models/message_spec.rb
@@ -123,6 +123,23 @@
@message.html_part.should == @email_template.html_part
@message.plain_part.should == @email_template.plain_part
end
+
+ it "should copy the email template's attachments over" do
+ @email_template = Factory(:email_template)
+ data = File.read(File.join(RAILS_ROOT, 'spec', 'resources', 'rails.png'))
+ attachment = Factory(:attachment,
+ :filename => 'rails.png',
+ :directory => 'image',
+ :data => data,
+ :content_type => 'image/png',
+ :message_id => @email_template.id)
+ @message = Message.new(:title => "New Email", :email_template => @email_template)
+ @message.state = 'template_selected'
+ @message.save
+ @message.next_step
+ @message.attachments.count.should == 1
+ @message.attachments.first.filename.should == 'rails.png'
+ end
it "should set return edit_content if the state is set to 'file_uploaded'" do
@message = Message.new
Please sign in to comment.
Something went wrong with that request. Please try again.