forked from oneclickorgs/one-click-orgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use state machine to manage Organisation lifecycle.
- Loading branch information
Showing
20 changed files
with
132 additions
and
90 deletions.
There are no files selected for viewing
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
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
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
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
55 changes: 55 additions & 0 deletions
55
db/migrate/20110521215633_convert_organisations_to_state_machine.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class ConvertOrganisationsToStateMachine < ActiveRecord::Migration | ||
class Organisation < ActiveRecord::Base;end | ||
class Clause < ActiveRecord::Base; end | ||
|
||
def self.up | ||
add_column :organisations, :state, :string | ||
|
||
Organisation.all.each do |organisation| | ||
state_clause = Clause.where( | ||
:organisation_id => organisation.id, | ||
:name => 'organisation_state', | ||
:ended_at => nil | ||
).order('started_at DESC').first | ||
|
||
if state_clause | ||
organisation.state = state_clause.text_value | ||
else | ||
organisation.state = 'pending' | ||
end | ||
|
||
organisation.save! | ||
end | ||
end | ||
|
||
def self.down | ||
Organisation.all.each do |organisation| | ||
state_clause = Clause.where( | ||
:organisation_id => organisation.id, | ||
:name => 'organisation_state', | ||
:ended_at => nil | ||
).order('started_at DESC').first | ||
|
||
if state_clause | ||
if state_clause.text_value != organisation.state | ||
state_clause.update_attribute(:ended_at, Time.now.utc) | ||
Clause.create!( | ||
:organisation_id => organisation.id, | ||
:name => 'organisation_state', | ||
:started_at => Time.now.utc, | ||
:text_value => organisation.state | ||
) | ||
end | ||
else | ||
Clause.create!( | ||
:organisation_id => organisation.id, | ||
:name => 'organisation_state', | ||
:started_at => Time.now.utc, | ||
:text_value => organisation.state | ||
) | ||
end | ||
end | ||
|
||
remove_column :organisations, :state | ||
end | ||
end |
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
before(:each) do | ||
default_organisation | ||
@organisation.active! | ||
default_constitution | ||
|
||
@user = login | ||
|
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
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
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
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
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
Oops, something went wrong.