Skip to content

Commit

Permalink
Adds foundation for basic housekeeping extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Nov 21, 2013
1 parent 4d851f9 commit c10cd2c
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -64,6 +64,7 @@ gem 'debugger', group: [:development, :test] if not win_os

group :development do
gem 'awesome_print'
gem 'foreigner', '~> 1.6'
end

gem "rspec-rails", :group => [:development, :test]
Expand All @@ -90,7 +91,6 @@ gem 'acts_as_list'
# bcrypt-ruby (3.1.2-x64-mingw32)
# bcrypt-ruby (3.1.2-x86-mingw32)


gem 'bcrypt-ruby', '~> 3.1.2'

# gem for dumping DwC-A archives
Expand Down
11 changes: 11 additions & 0 deletions app/models/concerns/shared/housekeeping.rb
@@ -0,0 +1,11 @@
module Shared::ProjectHouseKeeping

extend ActiveSupport::Concern

included do
belongs_to :project
belongs_to :creator
belongs_to :modifier
end

end
2 changes: 1 addition & 1 deletion app/models/source/verbatim.rb
@@ -1,2 +1,2 @@
class Source::Verbatim < ActiveRecord::Base
class Source::Verbatim < Source
end
9 changes: 9 additions & 0 deletions lib/housekeeping.rb
@@ -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
12 changes: 12 additions & 0 deletions lib/housekeeping/projects.rb
@@ -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

15 changes: 15 additions & 0 deletions lib/housekeeping/users.rb
@@ -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

32 changes: 32 additions & 0 deletions lib/tasks/houskeeping.rake
@@ -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
70 changes: 70 additions & 0 deletions spec/lib/housekeeping_spec.rb
@@ -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
19 changes: 19 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -43,3 +43,22 @@
# --seed 1234
# config.order = "random"
end


# A Model helper for testing concerns in models that have no tables.
# Example use:
#
# class WithUser < ActiveRecord::Base
# include SomeConcern
# extend FakeTable
# end
#
module FakeTable
def columns
@columns ||= []
end

def column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
end

0 comments on commit c10cd2c

Please sign in to comment.