Sha256: 9aa10770e3ddbb6a5b5ce788ae3f04ace1ae7fbbdb00019e2f1280faa31dbf4e

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require 'spec_helper'

module TableRenamable

  describe Model do

    before(:each) do

      Post.table_name = 'posts'

      Post.connection.create_table :posts, force: true do |t|
        t.string :title
        t.string :body
        t.integer :user_id
        t.timestamps
      end
      Post.reset_column_information

      Post.connection.execute("DROP TABLE IF EXISTS my_new_name")


      # users
      User.connection.create_table :users, force: true do |t|
        t.string :name
        t.string :description
        t.timestamps
      end

    end

    context 'with table rename' do

      before(:each) do
        # only do this once but it has to happen after the table is defined
        unless Post.included_modules.include?(TableRenamable::Model)
          Post.class_eval do

            include TableRenamable::Model

            deprecate_table_name(:posts, :my_new_name)
          end
        end
      end

      context '.deprecate_table_name' do

        it 'allows tables to be renamed without missing a beat' do

          Post.connection.rename_table(:posts, :my_new_name)

          lambda { Post.all }.should_not raise_error

        end

      end
    end

    context '.deprecate_columns' do


      it 'allows columns to be renamed without throwing errors' do
        # need to call this to get the columns loaded
        User.all


        User.class_eval do

          include TableRenamable::Model

          deprecate_columns(:name)
        end

        User.columns.collect(&:name).should_not include "name"
        User.column_names.should_not include "name"

      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_renamable-3.0.0 spec/lib/table_renamable/model_spec.rb
table_renamable-2.0.0 spec/lib/table_renamable/model_spec.rb
table_renamable-0.0.1 spec/lib/table_renamable/model_spec.rb