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.


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


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


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.

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:%[email protected]%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

Raises:

  • (ActiveRecord::ActiveRecordError)

    if cannot connect


125
126
127
128
129
130
131
# File 'lib/eaco/cucumber/active_record.rb', line 125

def connect!(config = self.configuration)
  unless ENV['VERBOSE']
    config = config.merge(min_messages: 'WARNING')
  end

  active_record.establish_connection(config)
end

#default_config_fileString

:nocov: This isn't ran by Travis as we set EACO_AR_CONFIG, so Coveralls raises a false positive.

Raises:

  • (Errno::ENOENT)

    if the configuration file is not found.


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/eaco/cucumber/active_record.rb', line 101

def default_config_file
  Pathname.new('features/active_record.yml').realpath

rescue Errno::ENOENT => error
  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
end

#define_schema!nil

Loads the defined #schema


138
139
140
# File 'lib/eaco/cucumber/active_record.rb', line 138

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.

See Also:

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

152
153
154
155
156
157
158
159
160
# File 'lib/eaco/cucumber/active_record.rb', line 152

def log_stdout(&block)
  stdout = Rake::Utils.capture_stdout(&block)

  stdout.split("\n").each do |line|
    logger.info line
  end

  nil
end

#loggerLogger


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