Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds foundation for basic housekeeping extensions.
- Loading branch information
Showing
9 changed files
with
170 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Shared::ProjectHouseKeeping | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
belongs_to :project | ||
belongs_to :creator | ||
belongs_to :modifier | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
class Source::Verbatim < ActiveRecord::Base | ||
class Source::Verbatim < Source | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
# Concerns for models that are project specific and that have creator/updators | ||
module Housekeeping | ||
extend ActiveSupport::Concern | ||
included do | ||
include Users | ||
include Projects | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Concern the provides housekeeping and related methods for models that belong_to a Project | ||
module Housekeeping::Projects | ||
|
||
extend ActiveSupport::Concern | ||
included do | ||
belongs_to :project | ||
end | ||
|
||
module ClassMethods | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
# Concern the provides housekeeping and related methods for models that belong_to a creator and updator | ||
module Housekeeping::Users | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
belongs_to :creator, foreign_key: :created_by_id | ||
belongs_to :updater, foreign_key: :updated_by_id | ||
end | ||
|
||
module ClassMethods | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# We'll add foreign keys with immigrant ultimately | ||
# https://github.com/jenseng/immigrant | ||
|
||
require 'fileutils' | ||
|
||
namespace :tw do | ||
namespace :development do | ||
desc 'generate housekeeping migration for all models with housekeeping concern' | ||
|
||
task :generate_housekeeping_migration => [:environment] do |t| | ||
|
||
# Ensure that we have all models loaded | ||
Rails.application.eager_load! | ||
|
||
mf = File.new(Rails.root + 'tmp/migration.tmp', 'w') | ||
|
||
puts "# known subclasses of ActiveRecord::Base #{ActiveRecord::Base.subclasses.collect{|a| a.name}.join(", ")}" | ||
|
||
ActiveRecord::Base.subclasses.each do |d| | ||
if d.ancestors.include?(Housekeeping::Users) | ||
puts "add_column :#{d.name.demodulize.underscore}s, :created_by_id, :integer, index: true" | ||
puts "add_column :#{d.name.underscore}s, :updated_by_id, :integer, index: true" | ||
end | ||
|
||
if d.ancestors.include?(Housekeeping::Projects) | ||
puts "add_column :#{d.name.underscore}s, :project_id, :integer, index: true" | ||
end | ||
end | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'spec_helper' | ||
|
||
|
||
describe 'Housekeeping::User' do | ||
|
||
context 'Users' do | ||
let(:instance) { | ||
stub_model HousekeepingTestClass::WithUser, id: 10 | ||
} | ||
|
||
context 'associations' do | ||
specify 'creator' do | ||
expect(instance).to respond_to(:creator) | ||
end | ||
|
||
specify 'updater' do | ||
expect(instance).to respond_to(:updater) | ||
end | ||
end | ||
end | ||
|
||
context 'Projects' do | ||
let(:instance) { | ||
stub_model HousekeepingTestClass::WithProject, id: 10 | ||
} | ||
|
||
context 'associations' do | ||
specify 'project' do | ||
expect(instance).to respond_to(:project) | ||
end | ||
end | ||
end | ||
|
||
|
||
# Don't repeat all tests, just make sure we get one of each extension. | ||
context 'Housekeeping' do | ||
let(:instance) { | ||
stub_model HousekeepingTestClass::WithBoth, id: 10 | ||
} | ||
|
||
context 'associations' do | ||
specify 'project' do | ||
expect(instance).to respond_to(:project) | ||
end | ||
|
||
specify 'updater' do | ||
expect(instance).to respond_to(:updater) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
module HousekeepingTestClass | ||
class WithBoth < ActiveRecord::Base | ||
extend FakeTable | ||
include Housekeeping | ||
end | ||
|
||
class WithUser < ActiveRecord::Base | ||
extend FakeTable | ||
include Housekeeping::Users | ||
end | ||
|
||
class WithProject < ActiveRecord::Base | ||
extend FakeTable | ||
include Housekeeping::Projects | ||
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