Module: Wagons::Wagon

Extended by:
ActiveSupport::Concern
Defined in:
lib/wagons/wagon.rb

Overview

A wagon is an extension to your application train running on Rails.

Wagons are built on Rails Engines. To change an engine to a wagon, simply include this module into your own Engine.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#all_dependenciesObject

Recursive depdencies on other wagons.



45
46
47
# File 'lib/wagons/wagon.rb', line 45

def all_dependencies
  dependencies.map { |dep| dep.all_dependencies + [dep] }.flatten.uniq
end

#app_requirementObject

The version requirement for the main application.



102
103
104
# File 'lib/wagons/wagon.rb', line 102

def app_requirement
  self.class.app_requirement
end

#dependenciesObject

Direct dependencies on other wagons.



38
39
40
41
42
# File 'lib/wagons/wagon.rb', line 38

def dependencies
  gemspec.dependencies.map(&:name).
                       select { |dep| dep =~ /\A#{Wagons.app_name}_/ }.
                       map { |dep| Wagons.find(dep) || fail("No wagon #{dep} found") }
end

#descriptionObject

Description from the gemspec.



33
34
35
# File 'lib/wagons/wagon.rb', line 33

def description
  gemspec.description
end

#existing_seedsObject

Hash of the seed models to their existing records.



81
82
83
84
85
# File 'lib/wagons/wagon.rb', line 81

def existing_seeds
  silence_stream(STDOUT) do
    SeedFuNdo.existing_seeds seed_fixtures
  end
end

#gem_nameObject

Name of the gem.



27
28
29
30
# File 'lib/wagons/wagon.rb', line 27

def gem_name
  class_name = self.class.name.demodulize.underscore
  engine_name.sub(/_#{class_name}$/, '')
end

#gemspecObject

Gem Specification.



50
51
52
# File 'lib/wagons/wagon.rb', line 50

def gemspec
  Gem::Specification.find_by_name(gem_name)
end

#labelObject

Human readable name.



17
18
19
# File 'lib/wagons/wagon.rb', line 17

def label
  gemspec.summary
end

#load_seedObject

Load seed data in db/fixtures.



71
72
73
# File 'lib/wagons/wagon.rb', line 71

def load_seed
  SeedFu.seed seed_fixtures
end

#load_tasks(app = self) ⇒ Object

Loads tasks into the main Rails application. Overwritten to only load own rake tasks, without install:migrations task from Rails::Engine



113
114
115
116
117
118
119
120
121
122
# File 'lib/wagons/wagon.rb', line 113

def load_tasks(app = self)
  if Rails::VERSION::MAJOR < 4
    railties.all { |r| r.load_tasks(app) }
  end

  extend Rake::DSL if defined? Rake::DSL
  self.class.rake_tasks.each { |block| instance_exec(app, &block) }
  paths['lib/tasks'].existent.sort.each { |ext| load(ext) }
  self
end

#migrate(version = nil) ⇒ Object

Run the migrations.



61
62
63
# File 'lib/wagons/wagon.rb', line 61

def migrate(version = nil)
  ActiveRecord::Migrator.migrate(migrations_paths, version)
end

#migrations_pathsObject

Paths for migration files.



107
108
109
# File 'lib/wagons/wagon.rb', line 107

def migrations_paths
  paths['db/migrate'].existent
end

#prepare_test_dbObject

Loads the migrations and seeds of this wagon and its dependencies.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wagons/wagon.rb', line 88

def prepare_test_db
  depts = all_dependencies + [self]

  # migrate
  depts.each { |d| d.migrate }

  # seed
  SeedFu.quiet = true unless ENV['VERBOSE']
  SeedFu.seed([ Rails.root.join('db/fixtures').to_s,
                Rails.root.join('db/fixtures/test').to_s ])
  depts.each { |d| d.load_seed }
end

#protect?Boolean

If true, this wagon may not be removed. Override as required. May return a string with a message, why the wagon must not be removed.

Returns:

  • (Boolean)


56
57
58
# File 'lib/wagons/wagon.rb', line 56

def protect?
  false
end

#revertObject

Revert the migrations.



66
67
68
# File 'lib/wagons/wagon.rb', line 66

def revert
  ActiveRecord::Migrator.migrate(migrations_paths, 0)
end

#unload_seedObject

Unload seed data in db/fixtures.



76
77
78
# File 'lib/wagons/wagon.rb', line 76

def unload_seed
  SeedFuNdo.unseed seed_fixtures
end

#versionObject

Version from the gemspec.



12
13
14
# File 'lib/wagons/wagon.rb', line 12

def version
  gemspec.version
end

#wagon_nameObject

Simple system name, without application prefix.



22
23
24
# File 'lib/wagons/wagon.rb', line 22

def wagon_name
  gem_name.sub(/^#{Wagons.app_name}_/, '')
end