Created
May 16, 2013 06:15
-
-
Save matthijsgroen/5589725 to your computer and use it in GitHub Desktop.
Useful files for DCI in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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