Skip to content

Instantly share code, notes, and snippets.

@matthijsgroen
Created May 16, 2013 06:15
Show Gist options
  • Save matthijsgroen/5589725 to your computer and use it in GitHub Desktop.
Save matthijsgroen/5589725 to your computer and use it in GitHub Desktop.
Useful files for DCI in ruby
# Use as alternative for spec_helper for testing roles, and contexts
rails_root = File.expand_path('../../', __FILE__)
$LOAD_PATH.unshift(rails_root) unless $LOAD_PATH.include?(rails_root)
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
require 'active_support/dependencies'
autoload_paths = ActiveSupport::Dependencies.autoload_paths
%w(app/models app/contexts app/roles).each do |path|
autoload_paths.push(path) unless autoload_paths.include?(path)
end
require 'active_record'
db_yml_file = File.expand_path('config/database.yml')
db_config = YAML.load_file(db_yml_file)
ActiveRecord::Base.establish_connection(db_config['test'])
# /app/contexts/sample_context.rb
class SampleContext
# Expose the 'play' as call and supply the 'props'
def self.call(user)
new(user).call
end
attr_reader :user, :website
# Assign all props and provide the actors with their roles
def initialize(user)
@user = user
# user is an actor playing the role of website creator
@user.extend WebsiteCreator
end
# let the play commence
def call
@website = @user.create_website
# expose the play so that the altered props can be taken of the stage
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment