Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from joshsoftware/master
Add generators to configure active_record and mongoid after install
- Loading branch information
Showing
12 changed files
with
222 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'rails/generators/named_base' | ||
|
||
module AASM | ||
module Generators | ||
class AASMGenerator < Rails::Generators::NamedBase | ||
|
||
source_root File.expand_path("../templates", __FILE__) | ||
argument :column_name, type: :string, default: 'aasm_state' | ||
|
||
desc "Generates a model with the given NAME (if one does not exist) with aasm " << | ||
"block and migration to add aasm_state column." | ||
|
||
hook_for :orm | ||
|
||
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,35 @@ | ||
module AASM | ||
module Generators | ||
module OrmHelpers | ||
|
||
def model_contents | ||
if column_name == 'aasm_state' | ||
<<RUBY | ||
include AASM | ||
aasm do | ||
end | ||
RUBY | ||
else | ||
<<RUBY | ||
include AASM | ||
aasm :column => '#{column_name}' do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
private | ||
|
||
def model_exists? | ||
File.exists?(File.join(destination_root, model_path)) | ||
end | ||
|
||
def model_path | ||
@model_path ||= File.join("app", "models", "#{file_path}.rb") | ||
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,38 @@ | ||
require 'rails/generators/active_record' | ||
require 'generators/aasm/orm_helpers' | ||
|
||
module ActiveRecord | ||
module Generators | ||
class AASMGenerator < ActiveRecord::Generators::Base | ||
include AASM::Generators::OrmHelpers | ||
|
||
argument :column_name, type: :string, default: 'aasm_state' | ||
|
||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
def copy_aasm_migration | ||
if model_exists? | ||
migration_template "migration_existing.rb", "db/migrate/add_aasm_state_to_#{table_name}.rb" | ||
else | ||
migration_template "migration.rb", "db/migrate/aasm_create_#{table_name}.rb" | ||
end | ||
end | ||
|
||
def generate_model | ||
invoke "active_record:model", [name], migration: false unless model_exists? | ||
end | ||
|
||
def inject_aasm_content | ||
content = model_contents | ||
|
||
class_path = if namespaced? | ||
class_name.to_s.split("::") | ||
else | ||
[class_name] | ||
end | ||
inject_into_class(model_path, class_path.last, content) if model_exists? | ||
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,8 @@ | ||
class AASMCreate<%= table_name.camelize %> < ActiveRecord::Migration | ||
def change | ||
create_table(:<%= table_name %>) do |t| | ||
t.string :<%= column_name %> | ||
t.timestamps null: false | ||
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,9 @@ | ||
class AddAASMTo<%= table_name.camelize %> < ActiveRecord::Migration | ||
def self.up | ||
add_column :<%= table_name %>, :<%= column_name %>, :string | ||
end | ||
|
||
def self.down | ||
remove_column :<%= table_name %>, :<%= column_name %> | ||
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,28 @@ | ||
require 'rails/generators/named_base' | ||
require 'generators/aasm/orm_helpers' | ||
|
||
module Mongoid | ||
module Generators | ||
class AASMGenerator < Rails::Generators::NamedBase | ||
include AASM::Generators::OrmHelpers | ||
|
||
argument :column_name, type: :string, default: 'aasm_state' | ||
|
||
def generate_model | ||
invoke "mongoid:model", [name] unless model_exists? | ||
end | ||
|
||
def inject_aasm_content | ||
inject_into_file model_path, model_contents, after: "include Mongoid::Document\n" if model_exists? | ||
end | ||
|
||
def inject_field_types | ||
inject_into_file model_path, migration_data, after: "include Mongoid::Document\n" if model_exists? | ||
end | ||
|
||
def migration_data | ||
" field :#{column_name}" | ||
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,39 @@ | ||
require 'spec_helper' | ||
require 'generator_spec' | ||
require 'generators/active_record/aasm_generator' | ||
|
||
describe ActiveRecord::Generators::AASMGenerator, type: :generator do | ||
destination File.expand_path("../../../tmp", __FILE__) | ||
|
||
before(:all) do | ||
prepare_destination | ||
end | ||
|
||
it "creates model with aasm block for default column_name" do | ||
run_generator %w(user) | ||
assert_file "app/models/user.rb", /include AASM\n\n aasm do\n end\n/ | ||
end | ||
|
||
it "creates model with aasm block for custom column_name" do | ||
run_generator %w(user state) | ||
assert_file "app/models/user.rb", /aasm :column => 'state' do\n end\n/ | ||
end | ||
|
||
it "creates model with aasm block for namespaced model" do | ||
run_generator %w(Admin::User state) | ||
assert_file "app/models/admin/user.rb", /aasm :column => 'state' do\n end\n/ | ||
end | ||
|
||
it "creates migration for model with aasm_column" do | ||
run_generator %w(post) | ||
assert_migration "db/migrate/aasm_create_posts.rb", /create_table(:posts) do |t|\n t.string :aasm_state\n/ | ||
end | ||
|
||
it "add aasm_column in existing model" do | ||
run_generator %w(job) | ||
assert_file "app/models/job.rb" | ||
run_generator %w(job) | ||
assert_migration "db/migrate/add_aasm_state_to_jobs.rb" | ||
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,33 @@ | ||
require 'spec_helper' | ||
require 'generator_spec' | ||
require 'generators/mongoid/aasm_generator' | ||
|
||
begin | ||
require "mongoid" | ||
|
||
describe Mongoid::Generators::AASMGenerator, type: :generator do | ||
destination File.expand_path("../../../tmp", __FILE__) | ||
|
||
before(:all) do | ||
prepare_destination | ||
end | ||
|
||
it "creates model with aasm block for default column_name" do | ||
run_generator %w(user) | ||
assert_file "app/models/user.rb", /include AASM\n\n aasm do\n end\n/ | ||
end | ||
|
||
it "creates model with aasm block for custom column_name" do | ||
run_generator %w(user state) | ||
assert_file "app/models/user.rb", /aasm :column => 'state' do\n end\n/ | ||
end | ||
|
||
it "creates model with aasm block for namespaced model" do | ||
run_generator %w(Admin::User state) | ||
assert_file "app/models/admin/user.rb", /aasm :column => 'state' do\n end\n/ | ||
end | ||
|
||
end | ||
rescue LoadError | ||
puts "Not running Mongoid specs because mongoid gem is not installed!!!" | ||
end |