Skip to content

Instantly share code, notes, and snippets.

@Fire-Dragon-DoL
Last active December 15, 2015 01:19
Show Gist options
  • Select an option

  • Save Fire-Dragon-DoL/5179034 to your computer and use it in GitHub Desktop.

Select an option

Save Fire-Dragon-DoL/5179034 to your computer and use it in GitHub Desktop.
Boat visible can be...
class Boat < ActiveRecord::Base
MAX_VISIBLES = 7
MEDIA_RESOURCES = [:profile, :home].freeze
MEDIA_DIR = Rails.root.join('public', 'media', 'boats').freeze
MENU_ACTIONS = [
:inside_views,
:outside_views,
:virtual_tours,
:video,
:layouts,
:tech_specs,
:catalogue
].freeze
validates :name, presence: true, uniqueness: { case_sensitive: false }
validate :visible_slots_available_only_if_visible?
has_many :tech_specs, order: 'position'
def visible?
self.visible
end
def number
@number ||= nil
if @number.nil? || self.name_changed?
if self.name.nil?
@number = ''
else
@number = self.name.split.first
end
end
@number
end
def suffix
@suffix ||= nil
if @suffix.nil? || self.name_changed?
if self.name.nil?
@suffix = ''
else
@suffix = self.name.split.second
end
end
@suffix
end
def full_name
"AP #{self.name.titleize}"
end
def to_s
self.name
end
def visible_slots_available_only_if_visible?
return true unless self.visible
visible_boats_amount = self.class::visibles.size
result = visible_boats_amount < self.class::MAX_VISIBLES
unless result
self.errors.add(:visible_slots_available_only_if_visible?,
"There are already #{ visible_boats_amount } over #{ self.class::MAX_VISIBLES }")
end
result
end
def has_resource?(resource)
unless self.class::MEDIA_RESOURCES.include?(resource.to_sym)
raise "Resource #{ resource } not in valid media resources"
end
self.send("has_resource_#{ resource }?")
end
def has_resource_profile?
File.exist?(self.class::MEDIA_DIR.join(self.to_s.parameterize, 'profile.png'))
end
def self.visibles
where(visible: true)
end
end
require 'spec_helper'
describe Boat do
context "visible" do
it "are not more than #{ Boat::MAX_VISIBLES }" do
boats = FactoryGirl.create_list(:boat, Boat::MAX_VISIBLES)
expect { FactoryGirl.create(:boat) }.to raise_error(ActiveRecord::RecordInvalid)
end
it "is not" do
boats = FactoryGirl.create_list(:boat, Boat::MAX_VISIBLES)
boats[0].visible = false
boats[0].save!
Boat.visibles.size.should be < Boat::MAX_VISIBLES
end
it "can be if there are less than #{ Boat::MAX_VISIBLES } boats visible" do
boats = FactoryGirl.create_list(:boat, Boat::MAX_VISIBLES)
boats[0].visible = false
boats[0].save!
print "----- visibles: #{ Boat.visibles.size }"
FactoryGirl.create(:boat).should be_valid # This is line 62
end
end
end
Failures:
1) Boat visible can be if there are less than 7 boats visible
Screenshot: /mnt/hgfs/d_wamp_www/austin/austinparker_it/tmp/capybara/screenshot_2013-03-17-01-43-15.348.png
Failure/Error: FactoryGirl.create(:boat).should be_valid
expected #<Boat id: 8, name: "Jarrod", created_at: "2013-03-17 00:43:15", updated_at: "2013-03-17 00:43:15", visible: true, short_description: "Velit eligendi sint voluptatem temporibus est fugit...", description: "Cupiditate maiores voluptatem quibusdam. Inventore ..."> to be valid, but got errors: Visible slots available only if visible? There are already 7 over 7
# ./spec/models/boat_spec.rb:62:in `block (3 levels) in <top (required)>'
Finished in 1.92 seconds
10 examples, 1 failure
Failed examples:
rspec ./spec/models/boat_spec.rb:57 # Boat visible can be if there are less than 7 boats visible
FactoryGirl.define do
factory :tech_spec_name do
sequence(:name) { |n| "#{ Faker::Lorem.word } #{ n }"}
end
factory :boat do
name { Faker::Name.first_name }
short_description { Faker::Lorem.paragraph(1) }
description { Faker::Lorem.paragraph }
visible { true }
factory :boat_with_tech_specs do
ignore do
tech_specs_count 5
end
after(:create) do |boat, evaluator|
FactoryGirl.create_list(:tech_spec, evaluator.tech_specs_count, boat: boat)
end
end
end
factory :tech_spec do
boat { FactoryGirl.create(:boat) }
tech_spec_name { FactoryGirl.create(:tech_spec_name) }
value { Faker::Lorem.word }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment