Sha256: 5d4e24536478f7d4c7e1585ed9bfa3fde19eac8254de41371d9aac3fb9eb4945

Contents?: true

Size: 1.56 KB

Versions: 17

Compression:

Stored size: 1.56 KB

Contents

module ModelBuilder
  def self.included(example_group)
    example_group.class_eval do
      before do
        @created_tables ||= []
      end

      after do
        drop_created_tables
      end
    end
  end

  def create_table(table_name, options = {}, &block)
    connection = ActiveRecord::Base.connection

    begin
      connection.execute("DROP TABLE IF EXISTS #{table_name}")
      connection.create_table(table_name, options, &block)
      @created_tables << table_name
      connection
    rescue Exception => e
      connection.execute("DROP TABLE IF EXISTS #{table_name}")
      raise e
    end
  end

  def define_model_class(class_name, &block)
    define_class(class_name, ActiveRecord::Base, &block)
  end

  def define_active_model_class(class_name, options = {}, &block)
    define_class(class_name) do
      include ActiveModel::Validations

      options[:accessors].each do |column|
        attr_accessor column.to_sym
      end

      if block_given?
        class_eval(&block)
      end
    end
  end

  def define_model(name, columns = {}, &block)
    class_name = name.to_s.pluralize.classify
    table_name = class_name.tableize

    create_table(table_name) do |table|
      columns.each do |name, type|
        table.column name, type
      end
    end

    define_model_class(class_name, &block)
  end

  def drop_created_tables
    connection = ActiveRecord::Base.connection

    @created_tables.each do |table_name|
      connection.execute("DROP TABLE IF EXISTS #{table_name}")
    end
  end
end

RSpec.configure do |config|
  config.include ModelBuilder
end

Version data entries

17 entries across 13 versions & 4 rubygems

Version Path
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/model_builder.rb
challah-0.9.1.beta.3 vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/model_builder.rb
devise_sociable-0.1.0 vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/model_builder.rb
challah-0.9.1.beta vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/model_builder.rb
challah-0.9.0 vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/model_builder.rb
shoulda-matchers-1.4.2 spec/support/model_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/model_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.4.1/spec/support/model_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/model_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/shoulda-matchers-1.4.1/spec/support/model_builder.rb
challah-0.8.3 vendor/bundle/gems/shoulda-matchers-1.4.1/spec/support/model_builder.rb
challah-0.8.3 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/model_builder.rb
shoulda-matchers-1.4.1 spec/support/model_builder.rb
shoulda-matchers-1.4.0 spec/support/model_builder.rb
challah-0.8.1 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/model_builder.rb
challah-rolls-0.1.0 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/model_builder.rb
shoulda-matchers-1.3.0 spec/support/model_builder.rb