Commit a2ffa1d3
Changed files (3)
app
models
app/models/creation.rb
@@ -38,6 +38,11 @@ class Creation < ActiveRecord::Base
favorites.find_or_create_by(user: user)
end
+ def publish_message_with(publisher)
+ message = "#{name} By #{user.name} on https://www.cakeside.com/creations/#{to_param}!"
+ publisher.update(message)
+ end
+
class << self
def search(query)
sql_search = "%#{query}%"
app/services/create_cake_command.rb
@@ -1,18 +1,19 @@
class CreateCakeCommand
- def initialize(context, current_user = context.current_user)
+ def initialize(context, current_user = context.current_user, publisher = TwitterPublisher.new)
@context = context
@current_user = current_user
end
def run(creation_attributes, category_id, tags)
- creation = @current_user.creations.create(creation_attributes)
- creation.categories << Category.find(category_id) if category_id
- @current_user.tag(creation, with: tags, on: :tags)
+ cake = @current_user.creations.create(creation_attributes)
+ cake.categories << Category.find(category_id) if category_id
+ @current_user.tag(cake, with: tags, on: :tags)
- if creation.save
- @context.create_cake_succeeded(creation)
+ if cake.save
+ publisher.publish(cake)
+ @context.create_cake_succeeded(cake)
else
- @context.create_cake_failed(creation)
+ @context.create_cake_failed(cake)
end
end
end
app/services/twitter_publisher.rb
@@ -0,0 +1,20 @@
+class TwitterPublisher
+ def initialize(configuration = ENV)
+ @configuration = configuration
+ end
+
+ def publish(target)
+ target.publish_message_with(create_client) unless Rails.env.test?
+ end
+
+ private
+
+ def create_client
+ Twitter::REST::Client.new do |config|
+ config.consumer_key = @configuration["TWITTER_CONSUMER_KEY"]
+ config.consumer_secret = @configuration["TWITTER_CONSUMER_SECRET"]
+ config.access_token = @configuration["TWITTER_ACCESS_TOKEN"]
+ config.access_token_secret = @configuration["TWITTER_ACCESS_SECRET"]
+ end
+ end
+end