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_CONFIG specify a different ActiveRecord configuration file

  • VERBOSE log to stderr

Defined Under Namespace

Classes: Department, Document, Position, User

Instance Method Summary collapse

Instance Method Details

#active_recordClass

Looks up ActiveRecord and sets the logger.

Returns:

  • (Class)

    ActiveRecord::Base



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_logIO

Log to stderr if VERBOSE is given, else log to features/active_record.log

Returns:

  • (IO)

    the log destination



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

#cleanObject

Drops all tables currently instantiated in the database.

See Also:



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_filePathname

Returns the currently configured configuration file. Override using the +EACO_AR_CONFIG’ envinronment variable.

Returns:

  • (Pathname)

    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

#configurationHash

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.

Returns:

  • (Hash)

    the current database configuration

See Also:

  • Eaco::Cucumber::ActiveRecord.{{#config_file}


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

Parameters:

  • config (Hash) (defaults to: self.configuration)

    the configuration to use, #configuration by default.

Returns:

  • (ActiveRecord::ConnectionAdapters::ConnectionPool)

Raises:

  • (ActiveRecord::ActiveRecordError)

    if cannot connect



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_fileString

Returns active_record.yml relative to this source file.

Returns:

  • (String)

    active_record.yml relative to this source file.

Raises:

  • (Errno::ENOENT)

    if the configuration file is not found.



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.message}.

    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

Returns:

  • (nil)


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.

Parameters:

  • block (Proc)

Returns:

  • (nil)

See Also:

  • Eaco::Cucumber::ActiveRecord.{Rake{Rake::Utils{Rake::Utils.capture_stdout}


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

#loggerLogger

Returns the logger configured, logging to #active_record_log.

Returns:



59
60
61
# File 'lib/eaco/cucumber/active_record.rb', line 59

def logger
  active_record.logger
end

#schemaObject

Defines the database schema for the World scenario.

See Also:



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