Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed Jul 24, 2009
0 parents commit dff69ae
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
h2. INSTALLATION:

<pre>
<code>
script/plugin install git://github.com/carlosantoniodasilva/remarkable_aasm.git
</code>
</pre>

h2. USAGE:

<pre>
<code>
describe User do
should_aasm :status do |aasm|
aasm.initial_state :pending
aasm.states [:pending, :active, :suspended, :deleted]
aasm.events [:activate, :suspend, :delete]
end
end
</code>
</pre>

h2. LICENSE:

(The MIT License)

Copyright (c) 2009

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

50 changes: 50 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'rubygems'
require 'rubygems/specification'
require 'rake'
require 'rake/gempackagetask'
require 'spec/rake/spectask'

GEM = "remarkable_aasm"
GEM_VERSION = "0.2.0"
SUMMARY = "AASM Remarkable Matchers"
AUTHOR = "Carlos A. da Silva"
EMAIL = "carlosantoniodasilva@gmail.com"
HOMEPAGE = "http://carlosantoniodasilva.wordpress.com"


spec = Gem::Specification.new do |s|
s.name = GEM
s.version = GEM_VERSION
s.platform = Gem::Platform::RUBY
s.summary = SUMMARY
s.require_paths = ['lib']
s.files = FileList['lib/**/*.rb' '[A-Z]*'].to_a

s.author = AUTHOR
s.email = EMAIL
s.homepage = HOMEPAGE

s.rubyforge_project = GEM # GitHub bug, gem isn't being build when this miss
end

Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = %w(-fs --color)
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
end

desc "Install the gem locally"
task :install => [:package] do
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
end

desc "Create a gemspec file"
task :make_spec do
File.open("#{GEM}.gemspec", "w") do |file|
file.puts spec.to_ruby
end
end

12 changes: 12 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if RAILS_ENV == "test"

require 'remarkable_activerecord'

require File.join(File.dirname(__FILE__), "lib", "remarkable_aasm")

require 'spec'
require 'spec/rails'

Remarkable.include_matchers!(Remarkable::AASM, Spec::Rails::Example::ModelExampleGroup)
end

7 changes: 7 additions & 0 deletions lib/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
en:
remarkable:
<%= name %>:
have_a_name:
description: "should have a name"
expectations:
has_a_name: "{{subject_name}} should have a name {{attribute}}"
11 changes: 11 additions & 0 deletions lib/pt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pt:
remarkable:
aasm:
aasm:
description: "agir como aasm com o atributo xxxxxxxxx"
expectations:
aasm_column: "estivesse configurado como aasm com o atributo xxxxxxxxx"
aasm_initial_state: "estivesse que o estado inicial fosse xxxxxxxxx, obtive yyyy"
aasm_states: "possuísse os estados xxxxxxxx, possui yyyyy"
aasm_events: "possuísse os eventos xxxxxxxx, possui yyyyy"

55 changes: 55 additions & 0 deletions lib/remarkable_aasm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Remarkable
module AASM
module Matchers
class AASM < Remarkable::ActiveRecord::Base
arguments :name

assertion :aasm?, :aasm_column?, :aasm_initial_state?, :aasm_states?, :aasm_events?

optionals :initial_state
optionals :states
optionals :events

protected

def aasm?
subject_class.respond_to?(:aasm_states)
end

def aasm_column?
subject_class.aasm_column == @name.to_sym
end

def aasm_initial_state?
return true unless @options.key?(:initial_state)
subject_class.aasm_initial_state == @options[:initial_state]
end

def aasm_states?
return true unless @options.key?(:states)

Array(@options[:states]).to_a.each do |state|
return false unless subject_class.aasm_states.include?(state)
end

true
end

def aasm_events?
return true unless @options.key?(:events)

Array(@options[:events]).each do |event|
return false unless subject_class.aasm_events.keys.include?(event)
end

true
end
end

def aasm(*args, &block)
AASM.new(*args, &block).spec(self)
end
end
end
end

100 changes: 100 additions & 0 deletions spec/model_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This is based on Remarkable based on Shoulda model builder for Test::Unit.
#
module ModelBuilder
def self.included(base)
return unless base.name =~ /^Spec/

base.class_eval do
after(:each) do
if @defined_constants
@defined_constants.each do |class_name|
Object.send(:remove_const, class_name)
end
end

if @created_tables
@created_tables.each do |table_name|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{table_name}")
end
end
end
end

base.extend ClassMethods
end

def create_table(table_name, &block)
connection = ActiveRecord::Base.connection

begin
connection.execute("DROP TABLE IF EXISTS #{table_name}")
connection.create_table(table_name, &block)
@created_tables ||= []
@created_tables << table_name
connection
rescue Exception => e
connection.execute("DROP TABLE IF EXISTS #{table_name}")
raise e
end
end

def define_constant(class_name, base, &block)
class_name = class_name.to_s.camelize

klass = Class.new(base)
Object.const_set(class_name, klass)

klass.class_eval(&block) if block_given?

@defined_constants ||= []
@defined_constants << class_name

klass
end

def define_model_class(class_name, &block)
define_constant(class_name, ActiveRecord::Base, &block)
end

def define_model(name, columns = {}, &block)
class_name = name.to_s.pluralize.classify
table_name = class_name.tableize

table = columns.delete(:table) || lambda {|table|
columns.each do |name, type|
table.column name, *type
end
}

create_table(table_name, &table)

klass = define_model_class(class_name, &block)
instance = klass.new

self.class.subject { instance } if self.class.respond_to?(:subject)
instance
end

module ClassMethods
# This is a macro to run validations of boolean optionals such as :allow_nil
# and :allow_blank. This macro tests all scenarios. The specs must have a
# define_and_validate method defined.
#
def create_optional_boolean_specs(optional, base, options={})
base.describe "with #{optional} option" do
it { should define_and_validate(options.merge(optional => true)).send(optional) }
it { should define_and_validate(options.merge(optional => false)).send(optional, false) }
it { should_not define_and_validate(options.merge(optional => true)).send(optional, false) }
it { should_not define_and_validate(options.merge(optional => false)).send(optional) }
end
end

def create_message_specs(base)
base.describe "with message option" do
it { should define_and_validate(:message => 'valid_message').message('valid_message') }
it { should_not define_and_validate(:message => 'not_valid').message('valid_message') }
end
end
end

end
107 changes: 107 additions & 0 deletions spec/remarkable_aasm_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require File.join(File.dirname(__FILE__), "spec_helper.rb")

require 'aasm'

create_table "users" do end

class User < ActiveRecord::Base
include AASM
aasm_column :state
aasm_initial_state :pending
aasm_state :passive
aasm_state :pending
aasm_state :active
aasm_state :suspended
aasm_state :deleted

aasm_event :register do
transitions :from => :passive, :to => :pending, :on_transition => :valid?
end

aasm_event :activate do
transitions :from => :pending, :to => :active
end

aasm_event :suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended
end

aasm_event :delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end
end

describe Remarkable::AASM do
describe "aasm column" do
it "should validate a model defines the aasm column" do
aasm(:state).matches?(User.new).should be_true
end

it "should validate a model doesn't respond to wrong aasm column" do
aasm(:status).matches?(User.new).should be_false
end
end

describe "aasm initial state" do
it "should validate a model defines the initial state" do
aasm(:state, :initial_state => :pending).matches?(User.new).should be_true
end

it "should validate a model doesn't define the wrong initial state" do
aasm(:state, :initial_state => :passive).matches?(User.new).should be_false
end
end

describe "aasm states" do
context "using one state as a symbol" do
it "should validate a model defines the state" do
aasm(:state, :states => :pending).matches?(User.new).should be_true
end

it "should validate a model doesn't define the state" do
aasm(:state, :states => :inactive).matches?(User.new).should be_false
end
end

context "using an array with one or more states" do
it "should validate a model defines all states" do
aasm(:state, :states => [:passive, :pending, :active, :suspended, :deleted]).matches?(User.new).should be_true
end

it "should validate all states in any order" do
aasm(:state, :states => [:passive, :pending, :active, :suspended, :deleted].shuffle).matches?(User.new).should be_true
end

it "should validate a model doesn't define a state" do
aasm(:state, :states => [:passive, :pending, :active, :suspended, :deleted, :inactive]).matches?(User.new).should be_false
end
end
end

describe "aasm events" do
context "using one event as a symbol" do
it "should validate a model defines the event" do
aasm(:state, :events => :activate).matches?(User.new).should be_true
end

it "should validate a model doesn't define the event" do
aasm(:state, :states => :inactivate).matches?(User.new).should be_false
end
end

context "using an array with one or more events" do
it "should validate a model defines all events" do
aasm(:state, :events => [:register, :activate, :suspend, :delete]).matches?(User.new).should be_true
end

it "should validate all eventes in any order" do
aasm(:state, :events => [:register, :activate, :suspend, :delete].shuffle).matches?(User.new).should be_true
end

it "should validate a model doesn't define a event" do
aasm(:state, :events => [:register, :activate, :suspend, :delete, :inactivate]).matches?(User.new).should be_false
end
end
end
end

Loading

0 comments on commit dff69ae

Please sign in to comment.