Skip to content

Commit

Permalink
Fix push_to_twitter bug and clean factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Francois committed Oct 2, 2013
1 parent 8d192b4 commit 747fc25
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 47 deletions.
6 changes: 2 additions & 4 deletions app/controllers/admin/notes_controller.rb
Expand Up @@ -44,23 +44,21 @@ def new_or_edit
flash[:error] = _("Error, you are not allowed to perform this action")
return(redirect_to :action => 'new')
end

message = "updated"
end

if @note.save
flash[:notice] = _("Note was successfully %s.", message)
if params[:note][:push_to_twitter] && @note.twitter_id.blank?
if params[:push_to_twitter] && @note.twitter_id.blank?
unless @note.send_to_twitter
flash[:notice] = nil
flash[:error] = _("Oooops something wrong happened")
end
end
redirect_to :action => 'new'
redirect_to action: 'new'
end
return
end

render 'new'
end

Expand Down
6 changes: 2 additions & 4 deletions app/models/note.rb
Expand Up @@ -82,13 +82,11 @@ def send_to_twitter

tweet = twitter.update(self.twitter_message, options)
self.twitter_id = tweet.attrs[:id_str]

self.save

self.user.update_twitter_profile_image(tweet.attrs[:user][:profile_image_url])
return true
true
rescue
return false
false
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/notes/new.html.erb
Expand Up @@ -14,7 +14,7 @@
<fieldset class="hidden-phone">
<legend><%= _("Publish settings") %></legend>
<%= twitter_disabled_message(this_blog, current_user) %>
<label for="note_push_to_twitter" class='checkbox'>
<label for="push_to_twitter" class='checkbox'>
<%= check_box_tag 'push_to_twitter', true, @note.twitter_id.blank?, {disabled: !twitter_available?(this_blog, current_user)} %>
<%= _("POSSE to Twitter")%>
</label>
Expand Down
28 changes: 24 additions & 4 deletions spec/controllers/admin/notes_controller_spec.rb
Expand Up @@ -79,13 +79,13 @@
it {expect(response).to redirect_to(controller: 'notes', action: 'new')}
it {expect(Note.count).to eq(2) }
end

context "With missing params" do
before :each do
before :each do
Note.delete_all
post :new, note: { }
end

it {expect(response).to render_template(controller: 'notes', action: 'edit')}
it {expect(Note.count).to eq(0) }
end
Expand All @@ -100,7 +100,7 @@
henri = create(:user, :login => 'henri', :profile => create(:profile_admin, :label => Profile::ADMIN))
request.session = { :user => henri.id }
Note.delete_all
@note = FactoryGirl.create(:note, :user_id => henri.id)
@note = create(:note, :user_id => henri.id)
end

it 'should render template destroy' do
Expand All @@ -117,4 +117,24 @@

end

describe :edit do
context "when push to twitter" do
it "call note to send to twitter" do
create(:blog_with_twitter)
henru = create(:user_admin, :with_twitter)

request.session = { user: henru.id }
expect(Note.count).to eq(0)

twitter_cli = double(twitter_cli)
Twitter::Client.should_receive(:new).and_return(twitter_cli)
Tweet = Struct.new(:attrs)
tweet = Tweet.new({id_str: '2344'})
twitter_cli.should_receive(:update).and_return(tweet)

post :new, note: { body: "Emphasis _mine_, arguments *strong*" }, push_to_twitter: "true"
expect(Note.first.twitter_id).to eq('2344')
end
end
end
end
82 changes: 48 additions & 34 deletions spec/factories.rb
Expand Up @@ -22,28 +22,37 @@
notify_on_new_articles false
notify_on_comments false
password 'top-secret'
association :resource, factory: :avatar
state 'active'
twitter '@getpublify'
profile
association :profile, factory: :profile
association :resource, factory: :avatar
association :text_filter, factory: :textile
end

factory :user_with_an_empty_profile, parent: :user do |u|
u.name "Doe"
u.nickname "John Doe"
u.twitter nil
u.association :resource, nil
end
factory :user_with_an_empty_profile do
name "Doe"
nickname "John Doe"
twitter nil
association :resource, nil
end

factory :user_with_a_full_profile, parent: :user do |u|
u.description "I am a poor lonesone factory generated user"
u.url "http://myblog.net"
u.msn "random@mail.com"
u.aim "randomaccount"
u.yahoo "anotherrandomaccount"
u.twitter "@random"
u.jabber "random@account.com"
factory :user_with_a_full_profile do
description "I am a poor lonesone factory generated user"
url "http://myblog.net"
msn "random@mail.com"
aim "randomaccount"
yahoo "anotherrandomaccount"
twitter "@random"
jabber "random@account.com"
end

factory :user_admin do
association :profile, factory: :profile_admin

trait :with_twitter do
twitter_oauth_token "oauth_token"
twitter_oauth_token_secret "oauth_token"
end
end
end

factory :article do
Expand Down Expand Up @@ -166,28 +175,33 @@
build_stubbed filter
end
end
end

factory :profile, :class => :profile do |l|
l.label {FactoryGirl.generate(:label)}
l.nicename 'Publify contributor'
l.modules [:dashboard, :profile]
factory :blog_with_twitter do
twitter_consumer_key "consumer_key"
twitter_consumer_secret "consumer_secret"
end
end

factory :profile_admin, parent: :profile do
label Profile::ADMIN
nicename 'Publify administrator'
modules [ :dashboard, :write, :articles, :pages, :feedback, :themes,
:customizesidebar, :users, :seo, :media, :settings, :profile, :notes ]
end
factory :profile, :class => :profile do
label {FactoryGirl.generate(:label)}
nicename 'Publify contributor'
modules [:dashboard, :profile]

factory :profile_publisher, :parent => :profile do |l|
l.label 'publisher'
l.nicename 'Blog publisher'
l.modules [:users, :dashboard, :write, :articles, :pages, :feedback, :media, :notes]
end
factory :profile_admin do
label Profile::ADMIN
nicename 'Publify administrator'
modules [ :dashboard, :write, :articles, :pages, :feedback, :themes,
:customizesidebar, :users, :seo, :media, :settings, :profile, :notes ]
end

factory :profile_publisher do
label 'publisher'
nicename 'Blog publisher'
modules [:users, :dashboard, :write, :articles, :pages, :feedback, :media, :notes]
end

factory :profile_contributor, :parent => :profile do |l|
factory :profile_contributor do
end
end

factory :category do |c|
Expand Down

0 comments on commit 747fc25

Please sign in to comment.