<commit_before>require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

ActiveRecord::Schema.define(:version => 1) do
  create_table :dogs, :force => true do |t|
    t.string :breed
    t.string :other_breed
    t.string :color
    t.string :name
    t.integer :age
  end

  create_table :cats, :force => true do |t|
    t.string :breed
  end
end

class Breed < ClassyEnum::Base; end
class Breed::GoldenRetriever < Breed; end
class Breed::Snoop < Breed; end
class Breed::Husky < Breed; end

class Color < ClassyEnum::Base; end
class Color::White < Color; end;
class Color::Black < Color; end;

class CatBreed < ClassyEnum::Base
  owner :cat

  def breed_color
    "#{cat.color} #{self}"
  end
end

class CatBreed::Abyssian < CatBreed; end
class CatBreed::Bengal < CatBreed; end
class CatBreed::Birman < CatBreed; end
class CatBreed::Persian < CatBreed; end

class Dog < ActiveRecord::Base; end

class DefaultDog < Dog
  classy_enum_attr :breed
end

class AllowBlankBreedDog < Dog
  classy_enum_attr :breed, :allow_blank => true
end

class AllowNilBreedDog < Dog
  classy_enum_attr :breed, :allow_nil => true
end

class OtherDog < Dog
  classy_enum_attr :other_breed, :enum => 'Breed'
end

describe DefaultDog do
  specify { DefaultDog.new(:breed => nil).should_not be_valid }
  specify { DefaultDog.new(:breed => '').should_not be_valid }

  context "with valid breed options" do
    [:golden_retriever, 'golden_retriever', Breed::GoldenRetriever.new, Breed::GoldenRetriever].each do |option|
      subject { DefaultDog.new(:breed => option) }
      it { should be_valid }
      its(:breed) { should be_a(Breed::GoldenRetriever) }
      its('breed.allow_blank') { should be_false }

      it 'stores the enum as a valid string representation' do
        subject.save!
        subject.reload
        subject.breed.should be_a(Breed::GoldenRetriever)
      end
    end
  end

  context "with invalid breed options" do
    subject { DefaultDog.new(:breed => :fake_breed) }
    it { should_not be_valid }
    it { should have(1).error_on(:breed) }
  end
end

describe "A ClassyEnum that allows blanks" do
  specify { AllowBlankBreedDog.new(:breed => nil).should be_valid }
  specify { AllowBlankBreedDog.new(:breed => '').should be_valid }

  context "with valid breed options" do
    subject { AllowBlankBreedDog.new(:breed => :golden_retriever) }
    it { should be_valid }
    its('breed.allow_blank') { should be_true }
  end

  context "with invalid breed options" do
    subject { AllowBlankBreedDog.new(:breed => :fake_breed) }
    it { should_not be_valid }
    it { should have(1).error_on(:breed) }
  end
end

describe "A ClassyEnum that allows nils" do
  specify { AllowNilBreedDog.new(:breed => nil).should be_valid }
  specify { AllowNilBreedDog.new(:breed => '').should_not be_valid }

  context "with valid breed options" do
    subject { AllowNilBreedDog.new(:breed => :golden_retriever) }
    it { should be_valid }
    its('breed.allow_blank') { should be_true }
  end

  context "with invalid breed options" do
    subject { AllowNilBreedDog.new(:breed => :fake_breed) }
    it { should_not be_valid }
    it { should have(1).error_on(:breed) }
  end
end

describe "A ClassyEnum that has a different field name than the enum" do
  subject { OtherDog.new(:other_breed => :snoop) }
  its(:other_breed) { should be_a(Breed::Snoop) }
end

class ActiveDog < Dog
  classy_enum_attr :color
  validates_uniqueness_of :name, :scope => :color
  scope :goldens, where(:breed => 'golden_retriever')
end

describe ActiveDog do
  context 'uniqueness on name' do
    subject { ActiveDog.new(:name => 'Kitteh', :breed => :golden_retriever, :color => :black) }
    it { should be_valid }

    context 'with existing kitteh' do<commit_msg>Fixing Rails 4 deprecation warning<commit_after>require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

ActiveRecord::Schema.define(:version => 1) do
  create_table :dogs, :force => true do |t|
    t.string :breed
    t.string :other_breed
    t.string :color
    t.string :name
    t.integer :age
  end

  create_table :cats, :force => true do |t|
    t.string :breed
  end
end

class Breed < ClassyEnum::Base; end
class Breed::GoldenRetriever < Breed; end
class Breed::Snoop < Breed; end
class Breed::Husky < Breed; end

class Color < ClassyEnum::Base; end
class Color::White < Color; end;
class Color::Black < Color; end;

class CatBreed < ClassyEnum::Base
  owner :cat

  def breed_color
    "#{cat.color} #{self}"
  end
end

class CatBreed::Abyssian < CatBreed; end
class CatBreed::Bengal < CatBreed; end
class CatBreed::Birman < CatBreed; end
class CatBreed::Persian < CatBreed; end

class Dog < ActiveRecord::Base; end

class DefaultDog < Dog
  classy_enum_attr :breed
end

class AllowBlankBreedDog < Dog
  classy_enum_attr :breed, :allow_blank => true
end

class AllowNilBreedDog < Dog
  classy_enum_attr :breed, :allow_nil => true
end

class OtherDog < Dog
  classy_enum_attr :other_breed, :enum => 'Breed'
end

describe DefaultDog do
  specify { DefaultDog.new(:breed => nil).should_not be_valid }
  specify { DefaultDog.new(:breed => '').should_not be_valid }

  context "with valid breed options" do
    [:golden_retriever, 'golden_retriever', Breed::GoldenRetriever.new, Breed::GoldenRetriever].each do |option|
      subject { DefaultDog.new(:breed => option) }
      it { should be_valid }
      its(:breed) { should be_a(Breed::GoldenRetriever) }
      its('breed.allow_blank') { should be_false }

      it 'stores the enum as a valid string representation' do
        subject.save!
        subject.reload
        subject.breed.should be_a(Breed::GoldenRetriever)
      end
    end
  end

  context "with invalid breed options" do
    subject { DefaultDog.new(:breed => :fake_breed) }
    it { should_not be_valid }
    it { should have(1).error_on(:breed) }
  end
end

describe "A ClassyEnum that allows blanks" do
  specify { AllowBlankBreedDog.new(:breed => nil).should be_valid }
  specify { AllowBlankBreedDog.new(:breed => '').should be_valid }

  context "with valid breed options" do
    subject { AllowBlankBreedDog.new(:breed => :golden_retriever) }
    it { should be_valid }
    its('breed.allow_blank') { should be_true }
  end

  context "with invalid breed options" do
    subject { AllowBlankBreedDog.new(:breed => :fake_breed) }
    it { should_not be_valid }
    it { should have(1).error_on(:breed) }
  end
end

describe "A ClassyEnum that allows nils" do
  specify { AllowNilBreedDog.new(:breed => nil).should be_valid }
  specify { AllowNilBreedDog.new(:breed => '').should_not be_valid }

  context "with valid breed options" do
    subject { AllowNilBreedDog.new(:breed => :golden_retriever) }
    it { should be_valid }
    its('breed.allow_blank') { should be_true }
  end

  context "with invalid breed options" do
    subject { AllowNilBreedDog.new(:breed => :fake_breed) }
    it { should_not be_valid }
    it { should have(1).error_on(:breed) }
  end
end

describe "A ClassyEnum that has a different field name than the enum" do
  subject { OtherDog.new(:other_breed => :snoop) }
  its(:other_breed) { should be_a(Breed::Snoop) }
end

class ActiveDog < Dog
  classy_enum_attr :color
  validates_uniqueness_of :name, :scope => :color
  scope :goldens, lambda { where(:breed => 'golden_retriever') }
end

describe ActiveDog do
  context 'uniqueness on name' do
    subject { ActiveDog.new(:name => 'Kitteh', :breed => :golden_retriever, :color => :black) }
    it { should be_valid }

    context 'with existing kitteh' do
{'fraction_non_alphanumeric': 0.11181938911022576, 'fraction_numerical': 0.001195219123505976, 'mean_word_length': 3.967678100263852, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3090641', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2597, 'n_words': 692}