diff --git a/app/controllers/admin/jobs_controller.rb b/app/controllers/admin/jobs_controller.rb index 72c93f18..7ab1f8f4 100644 --- a/app/controllers/admin/jobs_controller.rb +++ b/app/controllers/admin/jobs_controller.rb @@ -1,5 +1,10 @@ module Admin class JobsController < Admin::ApplicationController include AdministrateCustomization + def update + tweet = params[:job].delete(:tweet) + super + requested_resource.tweet if requested_resource.errors.none? && tweet + end end end diff --git a/app/controllers/admin/news_items_controller.rb b/app/controllers/admin/news_items_controller.rb index bc946264..30becaa7 100644 --- a/app/controllers/admin/news_items_controller.rb +++ b/app/controllers/admin/news_items_controller.rb @@ -1,5 +1,10 @@ module Admin class NewsItemsController < Admin::ApplicationController include AdministrateCustomization + def update + tweet = params[:news_item].delete(:tweet) + super + requested_resource.tweet if requested_resource.errors.none? && tweet + end end end diff --git a/app/models/event.rb b/app/models/event.rb index 69bfe53c..df6352de 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -32,7 +32,7 @@ def to_s end def tweet - TweetEventService.new(self).call if persisted? && state == "scheduled" + TweetModelService.new(self).tweet("Our next event: #{title}") if persisted? && state == "scheduled" end private diff --git a/app/models/job.rb b/app/models/job.rb index 18e496dc..ea94ceb6 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -19,4 +19,8 @@ class Job < ActiveRecord::Base presence: true, inclusion: { in: STATES } validates :author, presence: true + + def tweet + TweetModelService.new(self).tweet("Check out the new job posting: #{title}") if persisted? && state == "published" + end end diff --git a/app/models/news_item.rb b/app/models/news_item.rb index 1d213ac5..42ba055f 100644 --- a/app/models/news_item.rb +++ b/app/models/news_item.rb @@ -43,4 +43,7 @@ class NewsItem < ActiveRecord::Base to_param :title # Instance methods + def tweet + TweetModelService.new(self).tweet("We've got some news: #{title}") if persisted? && state == "published" + end end diff --git a/app/services/tweet_event_service.rb b/app/services/tweet_model_service.rb similarity index 68% rename from app/services/tweet_event_service.rb rename to app/services/tweet_model_service.rb index f13a89db..148d2d82 100644 --- a/app/services/tweet_event_service.rb +++ b/app/services/tweet_model_service.rb @@ -1,17 +1,20 @@ -class TweetEventService +class TweetModelService include Rails.application.routes.url_helpers - def initialize(event) - @event = event + attr_reader :model + + def initialize(model) + @model = model end def success? @success end - def call + def tweet(message) + message = "#{message}, #{url(model)}" twitter_client.update(message) - Rails.logger.info "Sent tweet: \"#{message}\"" + Rails.logger.info "Sent tweet: #{message}" @success = true rescue Twitter::Error Rails.logger.info "Tweet not sent" @@ -20,8 +23,8 @@ def call private - def message - "Our next event: #{@event.title}, #{event_url(@event)}" + def url(model) + send("#{model.model_name.name.downcase}_url", @model.id) end def twitter_client diff --git a/spec/fixtures/vcr_cassettes/tweet_event_service/success_.yml b/spec/fixtures/vcr_cassettes/tweet_model_service/success_.yml similarity index 99% rename from spec/fixtures/vcr_cassettes/tweet_event_service/success_.yml rename to spec/fixtures/vcr_cassettes/tweet_model_service/success_.yml index d266039c..b9a163c5 100644 --- a/spec/fixtures/vcr_cassettes/tweet_event_service/success_.yml +++ b/spec/fixtures/vcr_cassettes/tweet_model_service/success_.yml @@ -76,6 +76,6 @@ http_interactions: Monkeys","screen_name":"wiredmonkeys","location":"Montreal","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":4,"friends_count":7,"listed_count":0,"created_at":"Wed Mar 27 02:59:48 +0000 2013","favourites_count":0,"utc_offset":-10800,"time_zone":"Atlantic Time (Canada)","geo_enabled":false,"verified":false,"statuses_count":8,"lang":"fr","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/486738106293313537\/91k3rTEx_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/486738106293313537\/91k3rTEx_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1306342303\/1404882388","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}' - http_version: + http_version: recorded_at: Mon, 16 May 2016 23:15:01 GMT recorded_with: VCR 3.0.1 diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 7967c2eb..8da8c89a 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -3,10 +3,10 @@ RSpec.describe Event, type: :model do let(:event) { FactoryGirl.create(:event) } let(:talks) { FactoryGirl.create_list(:talk, 5, event: event) } - let(:tweet_service) { double(:service, call: true) } + let(:tweet_service) { double(:service, tweet: true) } before do - allow(TweetEventService).to receive(:new) { tweet_service } + allow(TweetModelService).to receive(:new) { tweet_service } end describe "attributes" do @@ -67,7 +67,7 @@ let(:event) { create(:event, state: "scheduled") } it "calls the callback" do - expect(tweet_service).to receive(:call) + expect(tweet_service).to receive(:tweet) event.tweet end end @@ -76,7 +76,7 @@ let(:event) { create(:event, state: "proposed") } it "does not calls the callback" do - expect(tweet_service).to_not receive(:call) + expect(tweet_service).to_not receive(:tweet) event.tweet end end @@ -85,7 +85,7 @@ let(:event) { build(:event, state: "scheduled") } it "does not calls the callback" do - expect(tweet_service).to_not receive(:call) + expect(tweet_service).to_not receive(:tweet) event.tweet end end diff --git a/spec/models/news_item_spec.rb b/spec/models/news_item_spec.rb index e1cb5571..ea46db05 100644 --- a/spec/models/news_item_spec.rb +++ b/spec/models/news_item_spec.rb @@ -8,6 +8,8 @@ # Unpublished let(:archived_item) { create(:news_item, :archived) } let(:draft_item) { create(:news_item, :draft) } + # Services + let(:tweet_service) { double(:service, call: true) } it "returns a sorted array of most recently published items first" do expect(NewsItem.published).to eq [recent_item, older_item] diff --git a/spec/services/tweet_event_service_spec.rb b/spec/services/tweet_model_service_spec.rb similarity index 81% rename from spec/services/tweet_event_service_spec.rb rename to spec/services/tweet_model_service_spec.rb index ba34cf2a..d8eb5da8 100644 --- a/spec/services/tweet_event_service_spec.rb +++ b/spec/services/tweet_model_service_spec.rb @@ -1,10 +1,10 @@ require "rails_helper" -RSpec.describe TweetEventService do +RSpec.describe TweetModelService do let(:event) { create(:event) } let(:service) { described_class.new(event) } - subject { service.tap(&:call) } + subject { service.tap { |m| m.tweet("Our next event: #{event.title}") } } its(:success?) { is_expected.to eq true }