diff --git a/app/controllers/public/contact_controller.rb b/app/controllers/public/contact_controller.rb
index c9a2491..3586d14 100644
--- a/app/controllers/public/contact_controller.rb
+++ b/app/controllers/public/contact_controller.rb
@@ -3,14 +3,24 @@ class ContactController < PublicController
needs_profile
def new
- @contact = Contact.new(params[:contact])
+ @contact
if request.post?
- if @contact.save
+ @contact = Contact.new(params[:contact])
+ @contact.dest = profile
+ @contact.city = City.exists?(params[:city]) ? City.find(params[:city]).name : _('Missing')
+ @contact.state = State.exists?(params[:state]) ? State.find(params[:state]).name : _('Missing')
+ if @contact.deliver
flash[:notice] = _('Contact successfully sent')
- redirect_to :controller => 'profile', :profile => profile.identifier
+ redirect_to :action => 'new'
else
flash[:notice] = _('Contact not sent')
end
+ else
+ if logged_in?
+ @contact = Contact.new(:name => user.name, :email => user.email)
+ else
+ @contact = Contact.new
+ end
end
end
diff --git a/app/models/contact.rb b/app/models/contact.rb
index 2527bc3..9a994d2 100644
--- a/app/models/contact.rb
+++ b/app/models/contact.rb
@@ -1,13 +1,37 @@
-class Contact < Task
+class Contact < ActiveRecord::Base #WithoutTable
+ tableless :columns => [
+ [:name, :string],
+ [:subject, :string],
+ [:message, :string],
+ [:email, :string],
+ [:state, :string],
+ [:city, :string]
+ ]
+ attr_accessor :dest
- validates_presence_of :target_id, :subject, :email, :message
- validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT
+ N_('Subject'); N_('Message'); N_('City and state'); N_('e-Mail'); N_('Name')
- acts_as_having_settings :field => :data
- settings_items :subject, :message, :city_and_state, :email, :phone
+ validates_presence_of :subject, :email, :message, :name
+ validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?})
- def description
- _('%s sent a new message') % (requestor ? requestor.name : _('Someone'))
+ def deliver
+ Contact::Sender.deliver_mail(self)
+ end
+
+ class Sender < ActionMailer::Base
+ def mail(contact)
+ emails = [contact.dest.contact_email] + contact.dest.admins.map{|i| i.email}
+ recipients emails
+ from "#{contact.name} <#{contact.email}>"
+ subject 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')
+ end
end
end
diff --git a/app/models/profile.rb b/app/models/profile.rb
index 9704f18..ef636e4 100644
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -514,4 +514,8 @@ class Profile < ActiveRecord::Base
!self.articles.count(:conditions => {:type => 'Blog'}).zero?
end
+ def admins
+ self.members_by_role(Profile::Roles.admin)
+ end
+
end
diff --git a/app/views/contact/new.rhtml b/app/views/contact/new.rhtml
index 9f37b7e..1db7cbd 100644
--- a/app/views/contact/new.rhtml
+++ b/app/views/contact/new.rhtml
@@ -4,13 +4,11 @@
<% labelled_form_for :contact, @contact do |f| %>
- <%= hidden_field_tag 'contact[target_id]', profile.id %>
- <%= hidden_field_tag 'contact[requestor_id]', (logged_in? ? current_user.person.id : nil) %>
- <%= required f.text_field(:subject) %>
- <%= required f.text_field(:email) %>
- <%= f.text_field :city_and_state %>
- <%= f.text_field :phone %>
- <%= required f.text_area(:message, :rows => 5) %>
+ <%= f.text_field :name %>
+ <%= f.text_field :email %>
+ <%= labelled_form_field _('City and state'), select_city(true) %>
+ <%= f.text_field :subject %>
+ <%= f.text_area :message, :rows => 10, :cols => 60 %>
<%= submit_button(:send, _('Send')) %>
diff --git a/app/views/contact/sender/mail.rhtml b/app/views/contact/sender/mail.rhtml
new file mode 100644
index 0000000..9bb30f3
--- /dev/null
+++ b/app/views/contact/sender/mail.rhtml
@@ -0,0 +1,12 @@
+<%= _('Name: %s') % @name %>
+<%= _('e-Mail: %s') % @email %>
+<%= _('City and state: %s-%s') % [@city, @state] %>
+
+<%= _('Message:') %>
+--
+
+<%= @message %>
+
+--
+<%= _('%s environment system') % @environment %>
+<%= @url %>
diff --git a/app/views/tasks/_contact.rhtml b/app/views/tasks/_contact.rhtml
deleted file mode 100644
index 0db37cb..0000000
--- a/app/views/tasks/_contact.rhtml
+++ /dev/null
@@ -1,18 +0,0 @@
-
<%= task.description %>
-
-<% form_for('task', task, :url => { :action => 'close', :id => task.id}) do |f| %>
-
- <%= hidden_field_tag(:decision, :finish) %>
-
- <% %w[ subject email phone city_and_state message ].each do |field| %>
- <% if task.respond_to?(field) and !task.send(field).nil? and !task.send(field).empty? %>
- <%= _(field.humanize) %> | <%= task.send(field) %> |
- <% end %>
- <% end %>
-
-
- <% button_bar do %>
- <%= submit_button(:ok, _('Ok!')) %>
- <% end %>
-
-<% end %>
diff --git a/public/stylesheets/controller_contact.css b/public/stylesheets/controller_contact.css
new file mode 100644
index 0000000..8381228
--- /dev/null
+++ b/public/stylesheets/controller_contact.css
@@ -0,0 +1,6 @@
+/*** SELECT CITY ***/
+
+#content .select_state_for_origin,
+#content .select_city_for_origin {
+ display: inline;
+}
diff --git a/test/functional/contact_controller_test.rb b/test/functional/contact_controller_test.rb
index cfb8864..be51105 100644
--- a/test/functional/contact_controller_test.rb
+++ b/test/functional/contact_controller_test.rb
@@ -43,33 +43,30 @@ class ContactControllerTest < Test::Unit::TestCase
assert_tag :tag => 'textarea', :attributes => { :name => 'contact[message]' }
end
- should 'add hidden field with target_id' do
- get :new, :profile => enterprise.identifier
- assert_tag :tag => 'input', :attributes => { :name => 'contact[target_id]', :value => enterprise.id, :type => 'hidden' }
+ should 'redirect back to contact page after send contact' do
+ post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all'}
+ assert_response :redirect
+ assert_redirected_to :action => 'new'
end
- should 'add requestor id if logged in' do
+ should 'fill email if user logged in' do
login_as(profile.identifier)
- @controller.stubs(:current_user).returns(profile.user)
get :new, :profile => enterprise.identifier
- assert_tag :tag => 'input', :attributes => { :name => 'contact[requestor_id]', :value => profile.id }
+ assert_tag :tag => 'input', :attributes => {:name => 'contact[email]', :value => profile.email}
end
- should 'nil requestor id if not logged in' do
+ should 'fill name if user logged in' do
+ login_as(profile.identifier)
get :new, :profile => enterprise.identifier
- assert_tag :tag => 'input', :attributes => { :name => 'contact[requestor_id]', :value => nil }
- end
-
- should 'redirect to profile page after contact' do
- post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :target_id => enterprise.id}
- assert_response :redirect
- assert_redirected_to :controller => 'profile', :profile => enterprise.identifier
+ assert_tag :tag => 'input', :attributes => {:name => 'contact[name]', :value => profile.name}
end
- should 'be able to send contact' do
- assert_difference Contact, :count do
- post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :target_id => enterprise.id}
- end
+ should 'define city and state' do
+ City.stubs(:find).returns(City.new(:name => 'Camaçari'))
+ State.stubs(:find).returns(State.new(:name => 'Bahia'))
+ post :new, :profile => enterprise.identifier, :contact => {:name => 'john', :subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :state => 1, :city => 1}
+ assert_equal 'Camaçari', assigns(:contact).city
+ assert_equal 'Bahia', assigns(:contact).state
end
end
diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb
index 0d5de8a..1328f76 100644
--- a/test/functional/tasks_controller_test.rb
+++ b/test/functional/tasks_controller_test.rb
@@ -152,15 +152,4 @@ class TasksControllerTest < Test::Unit::TestCase
assert_equal f, assigns(:ticket).target
end
- should 'list enterprise contacts' do
- ent = Enterprise.create!(:identifier => 'contact_test_enterprise', :name => 'Test contact enteprise')
- task = Contact.create!(:subject => 'test', :target_id => profile.id, :email => 'visitor@invalid.com', :message => 'Hi, all')
-
- login_as(profile.identifier)
- get :index, :profile => ent.identifier
-
- assert_includes assigns(:tasks), task
- assert_tag :tag => 'li', :attributes => { :class => 'task-Contact' }, :content => 'Someone sent a new message'
- end
-
end
diff --git a/test/unit/contact_sender_test.rb b/test/unit/contact_sender_test.rb
new file mode 100644
index 0000000..7f48fab
--- /dev/null
+++ b/test/unit/contact_sender_test.rb
@@ -0,0 +1,54 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ContactSenderTest < Test::Unit::TestCase
+ FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures'
+ CHARSET = "utf-8"
+
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+
+ @expected = TMail::Mail.new
+ @expected.set_content_type "text", "plain", { "charset" => CHARSET }
+ end
+
+ should 'be able to deliver mail' do
+ ent = Enterprise.new(:name => 'my enterprise', :identifier => 'myent', :environment => Environment.default)
+ ent.contact_email = 'contact@invalid.com'
+ c = Contact.new(:dest => ent, :subject => 'hi molly')
+ response = Contact::Sender.deliver_mail(c)
+ assert_equal c.email, response.from
+ assert_equal c.subject, response.subject
+ end
+
+ should 'deliver mail to contact_email' do
+ ent = Enterprise.new(:name => 'my enterprise', :identifier => 'myent', :environment => Environment.default)
+ ent.contact_email = 'contact@invalid.com'
+ c = Contact.new(:dest => ent)
+ response = Contact::Sender.deliver_mail(c)
+ assert_includes response.to, c.dest.contact_email
+ end
+
+ should 'deliver mail to admins of enterprise' do
+ admin = create_user('admin_test').person
+ ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :environment => Environment.default)
+ ent.contact_email = 'contact@invalid.com'
+ ent.add_admin(admin)
+ assert ent.save!
+ c = Contact.new(:dest => ent)
+ response = Contact::Sender.deliver_mail(c)
+ assert_includes response.to, admin.email
+ end
+
+ private
+
+ def read_fixture(action)
+ IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}")
+ end
+
+ def encode(subject)
+ quoted_printable(subject, CHARSET)
+ end
+
+end
diff --git a/test/unit/contact_test.rb b/test/unit/contact_test.rb
index fdb760b..a2da596 100644
--- a/test/unit/contact_test.rb
+++ b/test/unit/contact_test.rb
@@ -2,24 +2,39 @@ require File.dirname(__FILE__) + '/../test_helper'
class ContactTest < ActiveSupport::TestCase
- should 'have serialized data' do
- t = Contact.new
- t.data[:test] = 'test'
-
- assert_equal({:test => 'test'}, t.data)
- end
-
should 'validates required fields' do
contact = Contact.new
assert !contact.valid?
+
contact.subject = 'Hi'
assert !contact.valid?
+
+ contact.name = 'john'
+ assert !contact.valid?
+
contact.email = 'visitor@invalid.com'
assert !contact.valid?
+
contact.message = 'Hi, all'
- assert !contact.valid?
- contact.target = create_user('contact_user_test').person
- assert contact.save!
+ assert contact.valid?
+ end
+
+ should 'validates format of email only if not empty' do
+ contact = Contact.new
+ contact.valid?
+ assert_equal "Email can't be blank", contact.errors[:email]
+ end
+
+ should 'inicialize fields on instanciate' do
+ assert_nothing_raised ArgumentError do
+ Contact.new(:name => 'john', :email => 'contact@invalid.com')
+ end
+ end
+
+ should 'deliver message' do
+ ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :environment => Environment.default)
+ c = Contact.new(:name => 'john', :email => 'john@invalid.com', :subject => 'hi', :message => 'hi, all', :dest => ent)
+ assert c.deliver
end
end
diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb
index e0aad9c..7e6df8a 100644
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -1112,6 +1112,14 @@ class ProfileTest < Test::Unit::TestCase
assert_nil p.blog
end
+ should 'list admins' do
+ c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile')
+ p = create_user('mytestuser').person
+ c.add_admin(p)
+
+ assert_equal [p], c.admins
+ end
+
private
def assert_invalid_identifier(id)
diff --git a/vendor/plugins/access_control/lib/acts_as_accessible.rb b/vendor/plugins/access_control/lib/acts_as_accessible.rb
index 3f896d1..bffd303 100644
--- a/vendor/plugins/access_control/lib/acts_as_accessible.rb
+++ b/vendor/plugins/access_control/lib/acts_as_accessible.rb
@@ -27,6 +27,9 @@ class ActiveRecord::Base
def members
role_assignments.map(&:accessor).uniq
end
+ def members_by_role(role)
+ role_assignments.select{|i| i.role.key == role.key}.map(&:accessor).uniq
+ end
def roles
Role.find(:all).select do |r|
diff --git a/vendor/plugins/access_control/test/acts_as_accessor_test.rb b/vendor/plugins/access_control/test/acts_as_accessor_test.rb
index 5fe1c9d..af0c988 100644
--- a/vendor/plugins/access_control/test/acts_as_accessor_test.rb
+++ b/vendor/plugins/access_control/test/acts_as_accessor_test.rb
@@ -55,4 +55,13 @@ class ActAsAccessorTest < Test::Unit::TestCase
assert !a.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([role, a, res])
assert !a.remove_role(role, res)
end
+
+ def test_get_members_by_role
+ res = AccessControlTestResource.create!(:name => 'bla')
+ a = AccessControlTestAccessor.create!(:name => 'ze')
+ role = Role.create!(:name => 'another_content_author', :permissions => ['bli'])
+ assert a.add_role(role, res)
+ assert_equal [a], a.members_by_role(role)
+ end
+
end
diff --git a/vendor/plugins/active_record_tableless/MIT-LICENSE b/vendor/plugins/active_record_tableless/MIT-LICENSE
new file mode 100644
index 0000000..8eaf6db
--- /dev/null
+++ b/vendor/plugins/active_record_tableless/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/plugins/active_record_tableless/README b/vendor/plugins/active_record_tableless/README
new file mode 100644
index 0000000..a441fb8
--- /dev/null
+++ b/vendor/plugins/active_record_tableless/README
@@ -0,0 +1,30 @@
+ActiveRecordTableless
+=====================
+
+Rails plugin allowing ActiveRecord models to be used with validations but
+without being backed by a database table.
+
+
+Install
+=======
+
+script/plugin install git://github.com/robinsp/active_record_tableless.git
+
+
+Example
+=======
+
+class TablelessModel < ActiveRecord::Base
+ tableless :columns => [
+ [:email, :string],
+ [:password, :string],
+ [:password_confirmation]
+ ]
+
+ validates_presence_of :email, :password, :password_confirmation
+ validates_confirmation_of :password
+
+end
+
+
+Copyright (c) 2008 [name of plugin creator], released under the MIT license
diff --git a/vendor/plugins/active_record_tableless/Rakefile b/vendor/plugins/active_record_tableless/Rakefile
new file mode 100644
index 0000000..6dc7c30
--- /dev/null
+++ b/vendor/plugins/active_record_tableless/Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the active_record_tableless plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the active_record_tableless plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ActiveRecordTableless'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
diff --git a/vendor/plugins/active_record_tableless/init.rb b/vendor/plugins/active_record_tableless/init.rb
new file mode 100644
index 0000000..d145fdd
--- /dev/null
+++ b/vendor/plugins/active_record_tableless/init.rb
@@ -0,0 +1,2 @@
+# Include hook code here
+ActiveRecord::Base.send(:include, ActiveRecord::Tableless)
\ No newline at end of file
diff --git a/vendor/plugins/active_record_tableless/lib/active_record/tableless.rb b/vendor/plugins/active_record_tableless/lib/active_record/tableless.rb
new file mode 100644
index 0000000..289e95c
--- /dev/null
+++ b/vendor/plugins/active_record_tableless/lib/active_record/tableless.rb
@@ -0,0 +1,56 @@
+module ActiveRecord
+ module Tableless
+
+ def self.included(base)
+ # 'base' is assumed to be ActiveRecord::Base
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def tableless( options = {} )
+ include ActiveRecord::Tableless::InstanceMethods
+ raise "No columns defined" unless options.has_key?(:columns) && !options[:columns].empty?
+
+ self.extend(MetaMethods)
+
+ for column_args in options[:columns]
+ column( *column_args )
+ end
+
+ end
+ end
+
+ module MetaMethods
+ def columns()
+ @columns ||= []
+ end
+
+ def column(name, sql_type = nil, default = nil, null = true)
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+ reset_column_information
+ end
+
+ # Do not reset @columns
+ def reset_column_information
+ generated_methods.each { |name| undef_method(name) }
+ @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil
+ end
+ end
+
+ module InstanceMethods
+ def create_or_update
+ errors.empty?
+ end
+
+ def saved!(with_id = 1)
+ self.id = with_id
+
+ def self.new_record?
+ false
+ end
+ end
+ alias_method :exists!, :saved!
+ end
+
+ end
+end
\ No newline at end of file
diff --git a/vendor/plugins/active_record_tableless/tasks/active_record_tableless_tasks.rake b/vendor/plugins/active_record_tableless/tasks/active_record_tableless_tasks.rake
new file mode 100644
index 0000000..3402ce6
--- /dev/null
+++ b/vendor/plugins/active_record_tableless/tasks/active_record_tableless_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :active_record_tableless do
+# # Task goes here
+# end
diff --git a/vendor/plugins/active_record_tableless/test/active_record_tableless_test.rb b/vendor/plugins/active_record_tableless/test/active_record_tableless_test.rb
new file mode 100644
index 0000000..095f3e5
--- /dev/null
+++ b/vendor/plugins/active_record_tableless/test/active_record_tableless_test.rb
@@ -0,0 +1,75 @@
+require 'test/unit'
+require 'rubygems'
+require 'active_record'
+require File.dirname(__FILE__) + '/../lib/active_record/tableless'
+
+
+
+ActiveRecord::Base.establish_connection( {
+ :adapter => 'sqlite3',
+ :database => ":memory:",
+ :timeout => 500
+
+})
+
+ActiveRecord::Base.send(:include, ActiveRecord::Tableless)
+
+class TablelessModel < ActiveRecord::Base
+ tableless :columns => [
+ [:email, :string],
+ [:password, :string],
+ [:password_confirmation] ]
+
+ validates_presence_of :email, :password, :password_confirmation
+ validates_confirmation_of :password
+
+end
+
+class ActiveRecordTablelessTest < Test::Unit::TestCase
+
+ def setup
+ super
+ @valid_attributes = {
+ :email => "robin@bogus.com",
+ :password => "password",
+ :password_confirmation => "password"
+ }
+ end
+
+ def test_create
+ assert TablelessModel.create(@valid_attributes).valid?
+ end
+
+ def test_validations
+ # Just check a few validations to make sure we didn't break ActiveRecord::Validations::ClassMethods
+ assert_not_nil TablelessModel.create(@valid_attributes.merge(:email => "")).errors[:email]
+ assert_not_nil TablelessModel.create(@valid_attributes.merge(:password => "")).errors[:password]
+ assert_not_nil TablelessModel.create(@valid_attributes.merge(:password_confirmation => "")).errors[:password]
+ end
+
+ def test_save
+ assert TablelessModel.new(@valid_attributes).save
+ assert !TablelessModel.new(@valid_attributes.merge(:password => "no_match")).save
+ end
+
+ def test_valid?
+ assert TablelessModel.new(@valid_attributes).valid?
+ assert !TablelessModel.new(@valid_attributes.merge(:password => "no_match")).valid?
+ end
+
+
+ def test_exists!
+ m = TablelessModel.new(@valid_attributes)
+
+ assert_nil m.id
+ assert m.new_record?
+
+ m.exists!
+ assert_equal 1, m.id
+ assert !m.new_record?
+
+ m.exists!(250)
+ assert_equal 250, m.id
+ assert !m.new_record?
+ end
+end
--
libgit2 0.21.2