anonymous_active_record

Replacement for broken Class.new(ActiveRecord::Base)


Keywords
activerecord, anonymous, minitest, rspec, test
License
MIT
Install
gem install anonymous_active_record -v 1.0.8

Documentation

AnonymousActiveRecord

This library was ๐ŸŽฉ inspired by ๐ŸŽฉ, the Wolverine project, which implemented a clever workaround to the official non-support of anonymous classes by ActiveRecord.

Warning: Use of this gem is a security risk, due to the use of Ruby's eval. It is intended for use in a test suite, or other non-critical environment.

Project AnonymousActiveRecord
gem name anonymous_active_record
license License: MIT
download rank Downloads Today
version Version
dependencies Depfu
continuous integration Build Status
test coverage Test Coverage
maintainability Maintainability
code triage Open Source Helpers
FOSSA Licenses FOSSA Status
homepage on Github.com, on Railsbling.com
documentation on RDoc.info
Spread โ™กโ“›โ“žโ“ฅโ“”โ™ก ๐ŸŒ, ๐Ÿ‘ผ, :shipit:, Tweet Peter, ๐ŸŒน

Installation

Add this line to your application's Gemfile:

gem 'anonymous_active_record'

And then execute:

$ bundle

Or install it yourself as:

$ gem install anonymous_active_record

Compatibility

This gem is compatible with, as of Feb 2021: โ€ข Ruby 2.4, 2.5, 2.6, 2.7, 3.0, ruby-head

Usage

Require the library in your spec_helper or other test suite boot file.

require 'anonymous_active_record'

Let's say you want to write specs for a module, HasBalloon, which provides a method has_balloon?, and will be mixed into ActiveRecord classes.

module HasBalloon
  def has_balloon?
    name == 'Spot' # only Spot has a balloon
  end
end

This won't work (really!):

let(:ar_with_balloon) do
  Class.new(ActiveRecord::Base) do
    attr_accessor :name

    include HasBalloon
    def flowery_name
      "#{b_f}#{name}#{b_f}"
    end
    def b_f
      has_balloon? ? '๐ŸŽˆ' : '๐ŸŒธ'
    end
  end
end

So do this instead:

let(:ar_with_balloon) do
  AnonymousActiveRecord.generate(columns: ['name']) do
    include HasBalloon
    def flowery_name
      "#{b_f}#{name}#{b_f}"
    end
    def b_f
      has_balloon? ? '๐ŸŽˆ' : '๐ŸŒธ'
    end
  end
end
it 'can test the module' do
  expect(ar_with_balloon.new(name: 'Spot').flowery_name).to eq('๐ŸŽˆSpot๐ŸŽˆ')
  expect(ar_with_balloon.new(name: 'Not Spot').flowery_name).to eq('๐ŸŒธNot Spot๐ŸŒธ')
end

Generate Options

AnonymousActiveRecord.generate(
  table_name: 'a_table_name',
      # if table_name is not set klass_basename will be used to derive a unique random table_name
      # default is a unique random table name
  klass_basename: 'anons', # is default
  columns: ['name'],
      # columns default is [],
      # meaning class will have ['id', 'created_at', 'updated_at'], as the AR defaults
      # Optionally provide an array of hashes and thereby designate column type:
      # [{name: 'name', type: 'string'}, {name: 'baked_at', type: 'time'}]
  timestamps: true, # is default
  indexes: [{ columns: ['name'], unique: true }],
      # indexes default is [],
      # meaning class will have no indexes, as the AR defaults
      # Optionally provide an array of hashes of index options (similar to those used in Rails migrations):
      # [{columns: ['name'], unique: true}, {columns: ['baked_at']}]
  connection_params: { adapter: 'sqlite3', encoding: 'utf8', database: ':memory:' } # is default
  ) do
   # code which becomes part of the class definition
end

The block is optional.

Factory Options

AnonymousActiveRecord.factory(
  source_data: [{ name: 'Phil' }, { name: 'Vickie' }]
        # Array of hashes, where each hash represents a record that will be created
    # ... The rest of the options are the same as for generate, see above.
  ) do
  # same as for generate, see above.
end

The block is optional.

There is also a factory! method that will raise if the create fails, accomplished by calling create! instead of create.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/pboling/anonymous_active_record. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Code of Conduct

Everyone interacting in the AnonymousActiveRecord projectโ€™s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Versioning

This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency 'anonymous_active_record', '~> 0.0'

License

License: MIT

FOSSA Status