diff --git a/app/models/article.rb b/app/models/article.rb index 95939a8..56fd15a 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -5,7 +5,7 @@ class Article < ActiveRecord::Base :allow_members_to_edit, :translation_of_id, :language, :license_id, :parent_id, :display_posts_in_current_language, :category_ids, :posts_per_page, :moderate_comments, - :accept_comments, :feed, :published, :source, + :accept_comments, :feed, :published, :source, :source_name, :highlighted, :notify_comments, :display_hits, :slug, :external_feed_builder, :display_versions, :external_link, :image_builder, :show_to_followers diff --git a/app/models/suggest_article.rb b/app/models/suggest_article.rb index 24a3aa5..82f50b5 100644 --- a/app/models/suggest_article.rb +++ b/app/models/suggest_article.rb @@ -1,20 +1,15 @@ class SuggestArticle < Task - validates_presence_of :target_id, :article_name, :article_body + validates_presence_of :target_id validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? } + validates_associated :article_object settings_items :email, :type => String settings_items :name, :type => String - settings_items :article_name, :type => String - settings_items :article_body, :type => String - settings_items :article_abstract, :type => String - settings_items :article_parent_id, :type => String - settings_items :source, :type => String - settings_items :source_name, :type => String - settings_items :highlighted, :type => :boolean, :default => false settings_items :ip_address, :type => String settings_items :user_agent, :type => String settings_items :referrer, :type => String + settings_items :article, :type => Hash, :default => {} after_create :schedule_spam_checking @@ -28,24 +23,34 @@ class SuggestArticle < Task requestor ? "#{requestor.name}" : "#{name} (#{email})" end + def article_object + if @article_object.nil? + @article_object = article_type.new(article.merge({:profile => target}).except(:type)) + if requestor.present? + @article_object.author = requestor + else + @article_object.author_name = name + end + end + @article_object + end + + def article_type + (article[:type] || 'TinyMceArticle').constantize + end + def perform - task = TinyMceArticle.new - task.profile = target - task.name = article_name - task.author_name = name - task.body = article_body - task.abstract = article_abstract - task.parent_id = article_parent_id - task.source = source - task.source_name = source_name - task.highlighted = highlighted - task.save! + article_object.save! end def title _("Article suggestion") end + def article_name + article[:name] + end + def subject article_name end diff --git a/app/views/cms/suggest_an_article.html.erb b/app/views/cms/suggest_an_article.html.erb index 2307cb9..885db31 100644 --- a/app/views/cms/suggest_an_article.html.erb +++ b/app/views/cms/suggest_an_article.html.erb @@ -6,18 +6,18 @@ <%= labelled_form_for 'task' do |f| %> - <%= required labelled_form_field(_('Title'), text_field(:task, 'article_name', :size => 50)) %> + <%= required labelled_form_field(_('Title'), text_field('task[article]', 'name', :size => 50)) %> - <%= labelled_form_field(_('Source'), text_field(:task, 'source_name')) %> + <%= labelled_form_field(_('Source'), text_field('task[article]', 'source_name')) %> - <%= labelled_form_field(_('Source URL'), text_field(:task, 'source')) %> + <%= labelled_form_field(_('Source URL'), text_field('task[article]', 'source')) %> <% unless logged_in? %> <%= required labelled_form_field(_('Your name'), text_field(:task, 'name')) %> <%= required labelled_form_field(_('Email'), text_field(:task, 'email')) %> <% end %> - <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :object => :task, :abstract_method => 'article_abstract', :body_method => 'article_body'} %> + <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :object => 'task[article]'} %> <%= hidden_field_tag('back_to', @back_to) %> diff --git a/app/views/spam/_suggest_article.html.erb b/app/views/spam/_suggest_article.html.erb index a414f55..9e99760 100644 --- a/app/views/spam/_suggest_article.html.erb +++ b/app/views/spam/_suggest_article.html.erb @@ -7,13 +7,13 @@
diff --git a/app/views/tasks/_suggest_article_accept_details.html.erb b/app/views/tasks/_suggest_article_accept_details.html.erb index ecf7f3e..3c2429e 100644 --- a/app/views/tasks/_suggest_article_accept_details.html.erb +++ b/app/views/tasks/_suggest_article_accept_details.html.erb @@ -4,11 +4,14 @@ <%= labelled_form_field(_("Sent by: "), f.text_field(:name)) %><%= label_tag(_("Email: %s") % task.email) %>
<% end %> -<%= required labelled_form_field(_('Title'), f.text_field(:article_name, :size => 50)) %> -<%= labelled_form_field(_('Source'), f.text_field(:source_name)) %> -<%= labelled_form_field(_("Source URL"), f.text_field(:source)) %> -<%= select_profile_folder(_('Select the folder where the article must be published'), "tasks[#{task.id}][task][article_parent_id]", task.target) %> -<%= labelled_form_field(_('Highlight this article'), f.check_box(:highlighted)) %> +<%= f.fields_for 'article', OpenStruct.new(task.article) do |a| %> + <%= required labelled_form_field(_('Title'), a.text_field(:name, :size => 50)) %> + <%= labelled_form_field(_('Source'), a.text_field(:source_name)) %> + <%= labelled_form_field(_("Source URL"), a.text_field(:source)) %> -<%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :f => f, :abstract_method => 'article_abstract', :body_method => 'article_body', :lead_id => task.id} %> + <%= select_profile_folder(_('Select the folder where the article must be published'), "tasks[#{task.id}][task][article][parent_id]", task.target) %> + <%= labelled_form_field(_('Highlight this article'), a.check_box(:highlighted)) %> + + <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :f => a, :lead_id => task.id} %> +<% end %> diff --git a/test/factories.rb b/test/factories.rb index de0f8a3..dade9a8 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -454,7 +454,7 @@ module Noosfero::Factory end def defaults_for_suggest_article - { :name => 'Sender', :email => 'sender@example.com', :article_name => 'Some title', :article_body => 'some body text', :article_abstract => 'some abstract text'} + { :name => 'Sender', :email => 'sender@example.com', :article => {:name => 'Some title', :body => 'some body text', :abstract => 'some abstract text'}} end def defaults_for_comment(params = {}) diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 4b459a7..69a0ef4 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -1439,22 +1439,22 @@ class CmsControllerTest < ActionController::TestCase logout get :suggest_an_article, :profile => profile.identifier - assert_tag :tag => 'textarea', :attributes => { :name => /article_abstract/, :class => 'mceEditor' } - assert_tag :tag => 'textarea', :attributes => { :name => /article_body/, :class => 'mceEditor' } + assert_tag :tag => 'textarea', :attributes => { :name => /task\[article\]\[abstract\]/, :class => 'mceEditor' } + assert_tag :tag => 'textarea', :attributes => { :name => /task\[article\]\[body\]/, :class => 'mceEditor' } end should 'create a task suggest task to a profile' do c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) assert_difference 'SuggestArticle.count' do - post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article_name => 'some name', :article_body => 'some body', :email => 'some@localhost.com', :name => 'some name'} + post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article => {:name => 'some name', :body => 'some body'}, :email => 'some@localhost.com', :name => 'some name'} end end should 'create suggest task with logged in user as the article author' do c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) - post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article_name => 'some name', :article_body => 'some body'} + post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article => {:name => 'some name', :body => 'some body'}} assert_equal profile, SuggestArticle.last.requestor end diff --git a/test/functional/spam_controller_test.rb b/test/functional/spam_controller_test.rb index e38f259..d84f0d3 100644 --- a/test/functional/spam_controller_test.rb +++ b/test/functional/spam_controller_test.rb @@ -10,7 +10,7 @@ class SpamControllerTest < ActionController::TestCase @article = fast_create(TextileArticle, :profile_id => @community.id) @spam_comment = fast_create(Comment, :source_id => @article.id, :spam => true, :name => 'foo', :email => 'foo@example.com') - @spam_suggest_article = SuggestArticle.create!(:name => 'spammer', :article_name => 'Spam article', :email => 'spammer@shady.place', :article_body => "Something you don't need", :target => @community, :spam => true) + @spam_suggest_article = SuggestArticle.create!(:name => 'spammer', :article => {:name => 'Spam article', :body => "Something you don't need"}, :email => 'spammer@shady.place', :target => @community, :spam => true) login_as @profile.identifier end diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 8af8765..4938c24 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -264,11 +264,11 @@ class TasksControllerTest < ActionController::TestCase c = fast_create(Community) c.add_admin profile @controller.stubs(:profile).returns(c) - t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c) + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c) get :index - assert_tag :tag => 'textarea', :content => /test abstract/, :attributes => { :name => /article_abstract/, :class => 'mceEditor' } - assert_tag :tag => 'textarea', :content => /test body/, :attributes => { :name => /article_body/, :class => 'mceEditor' } + assert_tag :tag => 'textarea', :content => /test abstract/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[abstract\]/, :class => 'mceEditor' } + assert_tag :tag => 'textarea', :content => /test body/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[body\]/, :class => 'mceEditor' } end should 'create TinyMceArticle article after finish approve suggested article task' do @@ -276,7 +276,7 @@ class TasksControllerTest < ActionController::TestCase c = fast_create(Community) c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) @controller.stubs(:profile).returns(c) - t = SuggestArticle.create!(:article_name => 'test name', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c) + t = SuggestArticle.create!(:article => {:name => 'test name', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c) post :close, :tasks => {t.id => { :task => {}, :decision => "finish"}} assert_not_nil TinyMceArticle.find(:first) @@ -288,16 +288,13 @@ class TasksControllerTest < ActionController::TestCase c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) @controller.stubs(:profile).returns(c) t = SuggestArticle.new - t.article_name = 'test name' - t.article_body = 'test body' + t.article = {:name => 'test name', :body => 'test body', :source => 'http://test.com', :source_name => 'some source name'} t.name = 'some name' - t.source = 'http://test.com' - t.source_name = 'some source name' t.email = 'test@localhost.com' t.target = c t.save! - post :close, :tasks => {t.id => { :task => {:article_name => 'new article name', :article_body => 'new body', :source => 'http://www.noosfero.com', :source_name => 'new source', :name => 'new name'}, :decision => "finish"}} + post :close, :tasks => {t.id => { :task => {:article => {:name => 'new article name', :body => 'new body', :source => 'http://www.noosfero.com', :source_name => 'new source'}, :name => 'new name'}, :decision => "finish"}} assert_equal 'new article name', TinyMceArticle.find(:first).name assert_equal 'new name', TinyMceArticle.find(:first).author_name assert_equal 'new body', TinyMceArticle.find(:first).body @@ -310,7 +307,7 @@ class TasksControllerTest < ActionController::TestCase c = fast_create(Community) c.add_admin profile @controller.stubs(:profile).returns(c) - t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c) + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c) get :index assert_select "#tasks_#{t.id}_task_name" @@ -321,7 +318,7 @@ class TasksControllerTest < ActionController::TestCase c = fast_create(Community) c.add_admin profile @controller.stubs(:profile).returns(c) - t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :requestor => fast_create(Person), :target => c) + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :requestor => fast_create(Person), :target => c) get :index assert_select "#tasks_#{t.id}_task_name", 0 diff --git a/test/unit/suggest_article_test.rb b/test/unit/suggest_article_test.rb index fbed706..253822e 100644 --- a/test/unit/suggest_article_test.rb +++ b/test/unit/suggest_article_test.rb @@ -13,16 +13,9 @@ class SuggestArticleTest < ActiveSupport::TestCase should 'have the article_name' do t = SuggestArticle.new - assert !t.errors[:article_name.to_s].present? - t.valid? - assert t.errors[:article_name.to_s].present? - end - - should 'have the article_body' do - t = SuggestArticle.new - assert !t.errors[:article_body.to_s].present? - t.valid? - assert t.errors[:article_body.to_s].present? + assert !t.article_object.errors[:name].present? + t.article_object.valid? + assert t.article_object.errors[:name].present? end should 'have the email' do @@ -46,19 +39,12 @@ class SuggestArticleTest < ActiveSupport::TestCase assert t.errors[:target_id.to_s].present? end - should 'have the article_abstract' do + should 'have the article' do t = SuggestArticle.new - assert t.respond_to?(:article_abstract) - end - - should 'have the article_parent_id' do - t = SuggestArticle.new - assert t.respond_to?(:article_parent_id) - end - - should 'source be defined' do - t = SuggestArticle.new - assert t.respond_to?(:source) + assert t.respond_to?(:article_object) + assert !t.errors[:article_object].present? + t.valid? + assert t.errors[:article_object].present? end should 'create an article on with perfom method' do @@ -66,9 +52,7 @@ class SuggestArticleTest < ActiveSupport::TestCase name = 'some name' body = 'some body' abstract = 'some abstract' - t.article_name = name - t.article_body = body - t.article_abstract = abstract + t.article = {:name => name, :body => body, :abstract => abstract} t.target = @profile count = TinyMceArticle.count t.perform @@ -77,8 +61,7 @@ class SuggestArticleTest < ActiveSupport::TestCase should 'fill source name and URL into created article' do t = build(SuggestArticle, :target => @profile) - t.source_name = 'GNU project' - t.source = 'http://www.gnu.org/' + t.article.merge!({:source_name => 'GNU project', :source => 'http://www.gnu.org/'}) t.perform article = TinyMceArticle.last @@ -95,7 +78,7 @@ class SuggestArticleTest < ActiveSupport::TestCase should 'highlight created article' do t = build(SuggestArticle, :target => @profile) - t.highlighted = true + t.article[:highlighted] = true t.perform article = TinyMceArticle.last(:conditions => { :name => t.article_name}) # just to be sure @@ -132,19 +115,19 @@ class SuggestArticleTest < ActiveSupport::TestCase end should 'have target notification message' do - task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe') + task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe') assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}.*[\n]*.*to approve or reject/, task.target_notification_message) end should 'have target notification description' do - task = build(SuggestArticle,:target => @profile, :article_name => 'suggested article', :name => 'johndoe') + task = build(SuggestArticle,:target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe') assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, task.target_notification_description) end should 'deliver target notification message' do - task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com') + task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com') email = TaskMailer.target_notification(task, task.target_notification_message).deliver @@ -160,7 +143,7 @@ class SuggestArticleTest < ActiveSupport::TestCase should 'delegate spam detection to plugins' do Environment.default.enable_plugin(EverythingIsSpam) - t1 = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com') + t1 = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com') EverythingIsSpam.any_instance.expects(:check_for_spam) @@ -189,7 +172,7 @@ class SuggestArticleTest < ActiveSupport::TestCase should 'notify plugins of suggest_articles being marked as spam' do Environment.default.enable_plugin(SpamNotification) - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com') + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com') t.spam! process_delayed_job_queue @@ -200,7 +183,7 @@ class SuggestArticleTest < ActiveSupport::TestCase should 'notify plugins of suggest_articles being marked as ham' do Environment.default.enable_plugin(SpamNotification) - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com') + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com') t.ham! process_delayed_job_queue @@ -219,7 +202,7 @@ class SuggestArticleTest < ActiveSupport::TestCase end should 'log spammer ip after marking comment as spam' do - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com', :ip_address => '192.168.0.1') + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com', :ip_address => '192.168.0.1') t.spam! log = File.open('log/test_spammers.log') assert_match "SuggestArticle-id: #{t.id} IP: 192.168.0.1", log.read @@ -238,4 +221,13 @@ class SuggestArticleTest < ActiveSupport::TestCase assert_equal person.name, t.sender end + should 'create an event on perfom method' do + t = SuggestArticle.new + t.article = {:name => 'name', :body => 'body', :type => 'Event'} + t.target = @profile + assert_difference "Event.count" do + t.perform + end + end + end -- libgit2 0.21.2