Module: Eaco::Cucumber::ActiveRecord
- Extended by:
- ActiveRecord
- Included in:
- ActiveRecord
- Defined in:
- lib/eaco/cucumber/active_record.rb,
lib/eaco/cucumber/active_record/user.rb,
lib/eaco/cucumber/active_record/schema.rb,
lib/eaco/cucumber/active_record/document.rb,
lib/eaco/cucumber/active_record/position.rb,
lib/eaco/cucumber/active_record/department.rb,
lib/eaco/cucumber/active_record/user/designators.rb,
lib/eaco/cucumber/active_record/user/designators/user.rb,
lib/eaco/cucumber/active_record/user/designators/position.rb,
lib/eaco/cucumber/active_record/user/designators/department.rb,
lib/eaco/cucumber/active_record/user/designators/authenticated.rb
Overview
ActiveRecord configuration and connection.
Database configuration is looked up in features/active_record.yml by default. Logs are sent to features/active_record.log, truncating the file at each run.
Environment variables:
-
EACO_AR_CONFIGspecify a differentActiveRecordconfiguration file -
VERBOSElog tostderr
Defined Under Namespace
Classes: Department, Document, Position, User
Instance Method Summary collapse
-
#active_record ⇒ Class
Looks up ActiveRecord and sets the
logger. -
#active_record_log ⇒ IO
Log to stderr if
VERBOSEis given, else log tofeatures/active_record.log. -
#clean ⇒ Object
Drops all tables currently instantiated in the database.
-
#config_file ⇒ Pathname
The currently configured configuration file.
-
#configuration ⇒ Hash
Returns an Hash wit the database configuration.
-
#connect!(config = self.configuration) ⇒ ActiveRecord::ConnectionAdapters::ConnectionPool
Establish ActiveRecord connection using the given configuration hash.
-
#default_config_file ⇒ String
active_record.ymlrelative to this source file. -
#define_schema! ⇒ nil
Loads the defined #schema.
-
#log_stdout(&block) ⇒ nil
protected
Captures stdout emitted by the given
blockand logs it asinfomessages. -
#logger ⇒ Logger
The logger configured, logging to #active_record_log.
-
#schema ⇒ Object
Defines the database schema for the World scenario.
Instance Method Details
#active_record ⇒ Class
Looks up ActiveRecord and sets the logger.
39 40 41 42 43 |
# File 'lib/eaco/cucumber/active_record.rb', line 39 def active_record @_active_record ||= ::ActiveRecord::Base.tap do |active_record| active_record.logger = ::Logger.new(active_record_log).tap {|l| l.level = 0} end end |
#active_record_log ⇒ IO
Log to stderr if VERBOSE is given, else log to features/active_record.log
51 52 53 54 |
# File 'lib/eaco/cucumber/active_record.rb', line 51 def active_record_log @_active_record_log ||= ENV['VERBOSE'] ? $stderr : 'features/active_record.log'.tap {|f| File.open(f, "w+")} end |
#clean ⇒ Object
Drops all tables currently instantiated in the database.
11 12 13 14 15 |
# File 'lib/eaco/cucumber/active_record/schema.rb', line 11 ::ActiveRecord::Base.connection.tap do |connection| connection.tables.each do |table_name| connection.drop_table table_name end end |
#config_file ⇒ Pathname
Returns the currently configured configuration file. Override using the +EACO_AR_CONFIG’ envinronment variable.
89 90 91 |
# File 'lib/eaco/cucumber/active_record.rb', line 89 def config_file Pathname.new(ENV['EACO_AR_CONFIG'] || default_config_file) end |
#configuration ⇒ Hash
Returns an Hash wit the database configuration.
Caveat:the returned Hash has a custom .to_s method that formats the configuration as a pgsql:// URL.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/eaco/cucumber/active_record.rb', line 73 def configuration @_config ||= YAML.load(config_file.read).tap do |conf| def conf.to_s # :nocov: 'pgsql://%s:%s@%s/%s' % values_at( :username, :password, :hostname, :database ) # :nocov: end end end |
#connect!(config = self.configuration) ⇒ ActiveRecord::ConnectionAdapters::ConnectionPool
Establish ActiveRecord connection using the given configuration hash
123 124 125 126 127 128 129 |
# File 'lib/eaco/cucumber/active_record.rb', line 123 def connect!(config = self.configuration) unless ENV['VERBOSE'] config = config.merge(min_messages: 'WARNING') end active_record.establish_connection(config) end |
#default_config_file ⇒ String
Returns active_record.yml relative to this source file.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/eaco/cucumber/active_record.rb', line 98 def default_config_file Pathname.new('features/active_record.yml').realpath rescue Errno::ENOENT => error # :nocov: raise error.class.new, <<-EOF.squeeze(' ') #{error.}. Please define your Active Record database configuration in the default location, or specify your configuration file location by passing the `EACO_AR_CONFIG' environment variable. EOF # :nocov: end |
#define_schema! ⇒ nil
Loads the defined #schema
136 137 138 |
# File 'lib/eaco/cucumber/active_record.rb', line 136 def define_schema! log_stdout { load 'eaco/cucumber/active_record/schema.rb' } end |
#log_stdout(&block) ⇒ nil (protected)
Captures stdout emitted by the given block and logs it as info messages.
150 151 152 153 154 155 156 157 158 |
# File 'lib/eaco/cucumber/active_record.rb', line 150 def log_stdout(&block) stdout = Rake::Utils.capture_stdout(&block) stdout.split("\n").each do |line| logger.info line end nil end |
#logger ⇒ Logger
Returns the logger configured, logging to #active_record_log.
59 60 61 |
# File 'lib/eaco/cucumber/active_record.rb', line 59 def logger active_record.logger end |
#schema ⇒ Object
Defines the database schema for the World scenario.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/eaco/cucumber/active_record/schema.rb', line 23 ::ActiveRecord::Schema.define(version: '2015022301') do # Resource create_table 'documents', force: true do |t| t.string :name t.column :acl, :jsonb end # Actor create_table 'users', force: true do |t| t.string :name t.boolean :admin, default: false end # Designator source create_table 'departments', force: true do |t| t.string :name end add_index :departments, :name, unique: true # Designator source create_table 'positions', force: true do |t| t.string :name t.references :user t.references :department end end |