Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor validations #6815

Merged
merged 5 commits into from Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/spec/controllers/spree/api/base_controller_spec.rb
Expand Up @@ -75,7 +75,7 @@ def index
resource.valid? # get some errors
expect(subject).to receive(:index).and_raise(ActiveRecord::RecordInvalid.new(resource))
get :index, token: 'exception-message'
expect(json_response).to eql('exception' => "Validation failed: Name can't be blank, Price can't be blank, Shipping category can't be blank")
expect(json_response).to eql('exception' => "Validation failed: Name can't be blank, Shipping category can't be blank, Price can't be blank")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [145/120]

end

it "maps semantic keys to nested_attributes keys" do
Expand Down
9 changes: 5 additions & 4 deletions core/app/models/spree/address.rb
Expand Up @@ -7,16 +7,17 @@ class Address < Spree::Base

has_many :shipments, inverse_of: :address

validates :firstname, :lastname, :address1, :city, :country, presence: true
validates :zipcode, presence: true, if: :require_zipcode?
validates :phone, presence: true, if: :require_phone?
with_options presence: true do
validates :firstname, :lastname, :address1, :city, :country
validates :zipcode, if: :require_zipcode?
validates :phone, if: :require_phone?
end

validate :state_validate, :postal_code_validate

alias_attribute :first_name, :firstname
alias_attribute :last_name, :lastname


self.whitelisted_ransackable_attributes = %w[firstname lastname company]

def self.build_default
Expand Down
4 changes: 1 addition & 3 deletions core/app/models/spree/adjustment.rb
Expand Up @@ -26,9 +26,7 @@ class Adjustment < Spree::Base
belongs_to :source, polymorphic: true
belongs_to :order, class_name: 'Spree::Order', inverse_of: :all_adjustments

validates :adjustable, presence: true
validates :order, presence: true
validates :label, presence: true
validates :adjustable, :order, :label, presence: true
validates :amount, numericality: true

state_machine :state, initial: :open do
Expand Down
4 changes: 1 addition & 3 deletions core/app/models/spree/customer_return.rb
Expand Up @@ -9,9 +9,7 @@ class CustomerReturn < Spree::Base

after_create :process_return!

validates :return_items, presence: true
validates :stock_location, presence: true

validates :return_items, :stock_location, presence: true
validate :must_have_return_authorization, on: :create
validate :return_items_belong_to_same_order

Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/line_item.rb
Expand Up @@ -22,8 +22,8 @@ class LineItem < Spree::Base
}
validates :price, numericality: true
validates_with Stock::AvailabilityValidator

validate :ensure_proper_currency

before_destroy :update_inventory
before_destroy :destroy_inventory_units

Expand Down
6 changes: 4 additions & 2 deletions core/app/models/spree/option_type.rb
Expand Up @@ -9,8 +9,10 @@ class OptionType < Spree::Base
has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype'
has_many :prototypes, through: :option_type_prototypes, class_name: 'Spree::Prototype'

validates :name, presence: true, uniqueness: { allow_blank: true }
validates :presentation, presence: true
with_options presence: true do
validates :name, uniqueness: { allow_blank: true }
validates :presentation
end

default_scope { order(:position) }

Expand Down
6 changes: 4 additions & 2 deletions core/app/models/spree/option_value.rb
Expand Up @@ -6,8 +6,10 @@ class OptionValue < Spree::Base
has_many :option_value_variants, class_name: 'Spree::OptionValueVariant'
has_many :variants, through: :option_value_variants, class_name: 'Spree::Variant'

validates :name, presence: true, uniqueness: { scope: :option_type_id, allow_blank: true }
validates :presentation, presence: true
with_options presence: true do
validates :name, uniqueness: { scope: :option_type_id, allow_blank: true }
validates :presentation
end

after_touch :touch_all_variants

Expand Down
19 changes: 7 additions & 12 deletions core/app/models/spree/order.rb
Expand Up @@ -117,19 +117,14 @@ def states
before_create :link_by_email
before_update :homogenize_line_item_currencies, if: :currency_changed?

validates :number, presence: true,
length: { maximum: 32, allow_blank: true },
uniqueness: { allow_blank: true }
validates :email, presence: true,
length: { maximum: 254, allow_blank: true },
email: { allow_blank: true },
if: :require_email
validates :state, presence: true,
inclusion: { in: state_machine.states.map { |state| state.name.to_s }, allow_blank: true }
with_options presence: true do
validates :number, length: { maximum: 32, allow_blank: true }, uniqueness: { allow_blank: true }
validates :email, length: { maximum: 254, allow_blank: true }, email: { allow_blank: true }, if: :require_email
validates :state, inclusion: { in: state_machine.states.map { |state| state.name.to_s }, allow_blank: true }
validates :item_count, numericality: { greater_than_or_equal_to: 0, less_than: 2**31, only_integer: true, allow_blank: true }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [131/120]

end
validates :payment_state, inclusion: { in: %w[balance_due paid credit_owed failed void], allow_blank: true }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create array directly like ['balance_due', 'paid', 'etc' ] rather than %w[balance_due paid etc]
[] is faster that %w[]
Benchmark.bm do |x|
1.9.3-p551 :009 > x.report { %w[balance_due paid credit_owed failed void] * 10000 }
1.9.3-p551 :010?> x. report { ['balance_due' 'paid' 'credit_owed' 'failed' 'void'] * 10000}
1.9.3-p551 :011?> end
user system total real
0.000000 0.000000 0.000000 ( 0.000105)
0.000000 0.000000 0.000000 ( 0.000024)
=> [ 0.000000 0.000000 0.000000 ( 0.000105)
, 0.000000 0.000000 0.000000 ( 0.000024)
]
Correct me if i am wrong

validates :shipment_state, inclusion: { in: %w[ready pending partial shipped backorder canceled], allow_blank: true }
validates :item_count, presence: true,
numericality: { greater_than_or_equal_to: 0, less_than: 2**31, only_integer: true, allow_blank: true }
validates :item_total, POSITIVE_MONEY_VALIDATION
validates :adjustment_total, MONEY_VALIDATION
validates :included_tax_total, POSITIVE_MONEY_VALIDATION
Expand All @@ -141,7 +136,7 @@ def states

validate :has_available_shipment

delegate :update_totals, :persist_totals, :to => :updater
delegate :update_totals, :persist_totals, to: :updater
delegate :merge!, to: :merger
delegate :firstname, :lastname, to: :bill_address, prefix: true, allow_nil: true

Expand Down
13 changes: 8 additions & 5 deletions core/app/models/spree/product.rb
Expand Up @@ -87,11 +87,14 @@ class Product < Spree::Base
before_validation :normalize_slug, on: :update
before_validation :validate_master

validates :meta_keywords, length: { maximum: 255 }, allow_blank: true
validates :meta_title, length: { maximum: 255 }, allow_blank: true
validates :name, presence: true
validates :price, presence: true, if: proc { Spree::Config[:require_master_price] }
validates :shipping_category_id, presence: true
with_options length: { maximum: 255 }, allow_blank: true do
validates :meta_keywords
validates :meta_title
end
with_options presence: true do
validates :name, :shipping_category_id
validates :price, if: proc { Spree::Config[:require_master_price] }
end
validates :slug, length: { minimum: 3 }, allow_blank: true, uniqueness: true

attr_accessor :option_values_hash
Expand Down
12 changes: 6 additions & 6 deletions core/app/models/spree/refund.rb
Expand Up @@ -6,12 +6,12 @@ class Refund < Spree::Base

has_many :log_entries, as: :source

validates :payment, presence: true
validates :reason, presence: true
# can't require this on create because the perform! in after_create needs to run first
validates :transaction_id, presence: true, on: :update
validates :amount, presence: true, numericality: {greater_than: 0}

with_options presence: true do
validates :payment, :reason
# can't require this on create because the perform! in after_create needs to run first
validates :transaction_id, on: :update
validates :amount, numericality: { greater_than: 0 }
end
validate :amount_is_less_than_or_equal_to_allowed_amount, on: :create

after_create :perform!
Expand Down
4 changes: 1 addition & 3 deletions core/app/models/spree/return_authorization.rb
Expand Up @@ -15,9 +15,7 @@ class ReturnAuthorization < Spree::Base

accepts_nested_attributes_for :return_items, allow_destroy: true

validates :order, presence: true
validates :reason, presence: true
validates :stock_location, presence: true
validates :order, :reason, :stock_location, presence: true
validate :must_have_shipped_units, on: :create


Expand Down
16 changes: 9 additions & 7 deletions core/app/models/spree/stock_movement.rb
Expand Up @@ -5,13 +5,15 @@ class StockMovement < Spree::Base

after_create :update_stock_item_quantity

validates :stock_item, presence: true
validates :quantity, presence: true, numericality: {
greater_than_or_equal_to: -2**31,
less_than_or_equal_to: 2**31-1,
only_integer: true,
allow_nil: true
}
with_options presence: true do
validates :stock_item
validates :quantity, numericality: {
greater_than_or_equal_to: -2**31,
less_than_or_equal_to: 2**31 - 1,
only_integer: true,
allow_nil: true
}
end

scope :recent, -> { order(created_at: :desc) }

Expand Down
8 changes: 4 additions & 4 deletions core/app/models/spree/store.rb
Expand Up @@ -2,10 +2,10 @@ module Spree
class Store < Spree::Base
has_many :orders, class_name: "Spree::Order"

validates :code, presence: true, uniqueness: { allow_blank: true }
validates :name, presence: true
validates :url, presence: true
validates :mail_from_address, presence: true
with_options presence: true do
validates :code, uniqueness: { allow_blank: true }
validates :name, :url, :mail_from_address
end

before_save :ensure_default_exists_and_is_unique
before_destroy :validate_not_default
Expand Down
6 changes: 4 additions & 2 deletions core/app/models/spree/tax_rate.rb
Expand Up @@ -10,8 +10,10 @@ class TaxRate < Spree::Base
class_name: "Spree::TaxCategory",
inverse_of: :tax_rates

validates :amount, presence: true, numericality: { allow_nil: true }
validates :tax_category_id, presence: true
with_options presence: true do
validates :amount, numericality: { allow_nil: true }
validates :tax_category_id
end

scope :by_zone, -> (zone) { where(zone_id: zone.id) }
scope :potential_rates_for_zone,
Expand Down
8 changes: 5 additions & 3 deletions core/app/models/spree/taxon.rb
Expand Up @@ -20,9 +20,11 @@ class Taxon < Spree::Base
has_many :promotion_rules, through: :promotion_rule_taxons, class_name: 'Spree::PromotionRule'

validates :name, presence: true
validates :meta_keywords, length: { maximum: 255 }, allow_blank: true
validates :meta_description, length: { maximum: 255 }, allow_blank: true
validates :meta_title, length: { maximum: 255 }, allow_blank: true
with_options length: { maximum: 255 }, allow_blank: true do
validates :meta_keywords
validates :meta_description
validates :meta_title
end

after_save :touch_ancestors_and_taxonomy
after_touch :touch_ancestors_and_taxonomy
Expand Down
6 changes: 4 additions & 2 deletions core/app/models/spree/variant.rb
Expand Up @@ -34,8 +34,10 @@ class Variant < Spree::Base

validate :check_price

validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
validates :price, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
with_options numericality: { greater_than_or_equal_to: 0, allow_nil: true } do
validates :cost_price
validates :price
end
validates :sku, uniqueness: { conditions: -> { where(deleted_at: nil) } }, allow_blank: true

after_create :create_stock_items
Expand Down