diff --git a/app/mailers/contact.rb b/app/mailers/contact.rb new file mode 100644 index 0000000..8e53703 --- /dev/null +++ b/app/mailers/contact.rb @@ -0,0 +1,64 @@ +class Contact + + include ActiveModel::Validations + + def initialize(attributes = nil) + if attributes + attributes.each do |attr,value| + self.send("#{attr}=", value) + end + end + end + + attr_accessor :name + attr_accessor :subject + attr_accessor :message + attr_accessor :email + attr_accessor :state + attr_accessor :city + attr_accessor :receive_a_copy + attr_accessor :dest + attr_accessor :sender + + N_('Subject'); N_('Message'); N_('City and state'); N_('e-Mail'); N_('Name') + + validates_presence_of :subject, :email, :message, :name + validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) + + def deliver + return false unless self.valid? + Contact::Sender.notification(self).deliver + end + + class Sender < ActionMailer::Base + def notification(contact) + @name = contact.name + @email = contact.email + @city = contact.city + @state = contact.state + @message = contact.message + @environment = contact.dest.environment.name + @url = url_for(:host => contact.dest.environment.default_hostname, :controller => 'home') + @target = contact.dest.name + + options = { + content_type: 'text/html', + to: contact.dest.notification_emails, + reply_to: contact.email, + subject: "[#{contact.dest.short_name(30)}] " + contact.subject, + from: "#{contact.name} <#{contact.dest.environment.contact_email}>" + } + + if contact.sender + options.merge!(headers: {'X-Noosfero-Sender' => contact.sender.identifier}) + end + + if contact.receive_a_copy + options.merge!(cc: "#{contact.name} <#{contact.email}>") + end + + mail(options) + end + end + +end diff --git a/app/models/contact.rb b/app/models/contact.rb deleted file mode 100644 index 980a784..0000000 --- a/app/models/contact.rb +++ /dev/null @@ -1,58 +0,0 @@ -class Contact - - include ActiveModel::Validations - - def initialize(attributes = nil) - if attributes - attributes.each do |attr,value| - self.send("#{attr}=", value) - end - end - end - - attr_accessor :name - attr_accessor :subject - attr_accessor :message - attr_accessor :email - attr_accessor :state - attr_accessor :city - attr_accessor :receive_a_copy - attr_accessor :dest - attr_accessor :sender - - N_('Subject'); N_('Message'); N_('City and state'); N_('e-Mail'); N_('Name') - - validates_presence_of :subject, :email, :message, :name - validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) - - def deliver - return false unless self.valid? - Contact::Sender.deliver_mail(self) - end - - class Sender < ActionMailer::Base - def mail(contact) - content_type 'text/html' - emails = contact.dest.notification_emails - recipients emails - from "#{contact.name} <#{contact.dest.environment.contact_email}>" - reply_to contact.email - if contact.sender - headers 'X-Noosfero-Sender' => contact.sender.identifier - end - if contact.receive_a_copy - cc "#{contact.name} <#{contact.email}>" - end - subject "[#{contact.dest.short_name(30)}] " + contact.subject - body :name => contact.name, - :email => contact.email, - :city => contact.city, - :state => contact.state, - :message => contact.message, - :environment => contact.dest.environment.name, - :url => url_for(:host => contact.dest.environment.default_hostname, :controller => 'home'), - :target => contact.dest.name - end - end - -end diff --git a/app/views/contact/sender/mail.html.erb b/app/views/contact/sender/mail.html.erb deleted file mode 100644 index 817205d..0000000 --- a/app/views/contact/sender/mail.html.erb +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - -

<%= _('This message was sent by %{sender} to %{target} on %{environment}.') % - {:sender => @name, :target => @target, :environment => @environment} %>

- <%= _('Information about the user who sent this message:')%> - -
- <%= content_tag('b', _('Message:')) %> -

<%= word_wrap(@message) %>

- - --
- <%= _('Greetings,') %>
- <%= _('%s team.') % @environment %>
- <%= @url %> - - - diff --git a/app/views/contact/sender/notification.html.erb b/app/views/contact/sender/notification.html.erb new file mode 100644 index 0000000..817205d --- /dev/null +++ b/app/views/contact/sender/notification.html.erb @@ -0,0 +1,27 @@ + + + + + + +

<%= _('This message was sent by %{sender} to %{target} on %{environment}.') % + {:sender => @name, :target => @target, :environment => @environment} %>

+ <%= _('Information about the user who sent this message:')%> + +
+ <%= content_tag('b', _('Message:')) %> +

<%= word_wrap(@message) %>

+ + --
+ <%= _('Greetings,') %>
+ <%= _('%s team.') % @environment %>
+ <%= @url %> + + + diff --git a/test/unit/contact_test.rb b/test/unit/contact_test.rb index 6d0b2e2..b14bea7 100644 --- a/test/unit/contact_test.rb +++ b/test/unit/contact_test.rb @@ -22,7 +22,7 @@ class ContactTest < ActiveSupport::TestCase should 'validates format of email only if not empty' do contact = Contact.new contact.valid? - assert_match(/can't be blank/, contact.errors[:email]) + assert_match(/can't be blank/, contact.errors[:email].first) end should 'inicialize fields on instanciate' do @@ -62,7 +62,7 @@ class ContactTest < ActiveSupport::TestCase ent = fast_create(Enterprise, :name => 'my enterprise', :identifier => 'myent') c = Contact.new(:name => 'john', :email => 'john@invalid.com', :subject => 'hi', :message => 'hi, all', :dest => ent) email = c.deliver - assert_equal c.email, email.reply_to.to_s + assert_equal c.email, email.reply_to.first.to_s end end -- libgit2 0.21.2