Use boolean comparators where it makes sense
This commit is contained in:
parent
dbdc7ae4aa
commit
118886344a
28 changed files with 60 additions and 60 deletions
|
|
@ -103,13 +103,13 @@ class Article < ActiveRecord::Base
|
|||
def shared_article_changed?(supplier = self.supplier)
|
||||
# skip early if the timestamp hasn't changed
|
||||
shared_article = self.shared_article(supplier)
|
||||
unless shared_article.nil? or self.shared_updated_on == shared_article.updated_on
|
||||
unless shared_article.nil? || self.shared_updated_on == shared_article.updated_on
|
||||
|
||||
# try to convert units
|
||||
# convert supplier's price and unit_quantity into fc-size
|
||||
new_price, new_unit_quantity = self.convert_units
|
||||
new_unit = self.unit
|
||||
unless new_price and new_unit_quantity
|
||||
unless new_price && new_unit_quantity
|
||||
# if convertion isn't possible, take shared_article-price/unit_quantity
|
||||
new_price, new_unit_quantity, new_unit = shared_article.price, shared_article.unit_quantity, shared_article.unit
|
||||
end
|
||||
|
|
@ -142,7 +142,7 @@ class Article < ActiveRecord::Base
|
|||
# compare attributes from different articles. used for auto-synchronization
|
||||
# returns array of symbolized unequal attributes
|
||||
def self.compare_attributes(attributes)
|
||||
unequal_attributes = attributes.select { |name, values| values[0] != values[1] and not (values[0].blank? and values[1].blank?) }
|
||||
unequal_attributes = attributes.select { |name, values| values[0] != values[1] && !(values[0].blank? && values[1].blank?) }
|
||||
unequal_attributes.collect { |pair| pair[0] }
|
||||
end
|
||||
|
||||
|
|
@ -160,10 +160,10 @@ class Article < ActiveRecord::Base
|
|||
def convert_units
|
||||
if unit != shared_article.unit
|
||||
# legacy, used by foodcoops in Germany
|
||||
if shared_article.unit == "KI" and unit == "ST" # 'KI' means a box, with a different amount of items in it
|
||||
if shared_article.unit == "KI" && unit == "ST" # 'KI' means a box, with a different amount of items in it
|
||||
# try to match the size out of its name, e.g. "banana 10-12 St" => 10
|
||||
new_unit_quantity = /[0-9\-\s]+(St)/.match(shared_article.name).to_s.to_i
|
||||
if new_unit_quantity and new_unit_quantity > 0
|
||||
if new_unit_quantity && new_unit_quantity > 0
|
||||
new_price = (shared_article.price/new_unit_quantity.to_f).round(2)
|
||||
[new_price, new_unit_quantity]
|
||||
else
|
||||
|
|
@ -172,7 +172,7 @@ class Article < ActiveRecord::Base
|
|||
else # use ruby-units to convert
|
||||
fc_unit = (::Unit.new(unit) rescue nil)
|
||||
supplier_unit = (::Unit.new(shared_article.unit) rescue nil)
|
||||
if fc_unit and supplier_unit and fc_unit =~ supplier_unit
|
||||
if fc_unit && supplier_unit && fc_unit =~ supplier_unit
|
||||
conversion_factor = (supplier_unit / fc_unit).to_base.to_r
|
||||
new_price = shared_article.price / conversion_factor
|
||||
new_unit_quantity = shared_article.unit_quantity * conversion_factor
|
||||
|
|
@ -225,7 +225,7 @@ class Article < ActiveRecord::Base
|
|||
matches = Article.where(name: name, supplier_id: supplier_id, deleted_at: deleted_at, type: type)
|
||||
matches = matches.where.not(id: id) unless new_record?
|
||||
# supplier should always be there - except, perhaps, on initialization (on seeding)
|
||||
if supplier and (supplier.shared_sync_method.blank? or supplier.shared_sync_method == 'import')
|
||||
if supplier && (supplier.shared_sync_method.blank? || supplier.shared_sync_method == 'import')
|
||||
errors.add :name, :taken if matches.any?
|
||||
else
|
||||
errors.add :name, :taken_with_unit if matches.where(unit: unit, unit_quantity: unit_quantity).any?
|
||||
|
|
|
|||
|
|
@ -19,14 +19,14 @@ class ArticleCategory < ActiveRecord::Base
|
|||
# Find a category that matches a category name; may return nil.
|
||||
# TODO more intelligence like remembering earlier associations (global and/or per-supplier)
|
||||
def self.find_match(category)
|
||||
return if category.blank? or category.length < 3
|
||||
return if category.blank? || category.length < 3
|
||||
c = nil
|
||||
## exact match - not needed, will be returned by next query as well
|
||||
#c ||= ArticleCategory.where(name: category).first
|
||||
# case-insensitive substring match (take the closest match = shortest)
|
||||
c = ArticleCategory.where('name LIKE ?', "%#{category}%") unless c and c.any?
|
||||
c = ArticleCategory.where('name LIKE ?', "%#{category}%") unless c && c.any?
|
||||
# case-insensitive phrase present in category description
|
||||
c = ArticleCategory.where('description LIKE ?', "%#{category}%").select {|s| s.description.match /(^|,)\s*#{category}\s*(,|$)/i} unless c and c.any?
|
||||
c = ArticleCategory.where('description LIKE ?', "%#{category}%").select {|s| s.description.match /(^|,)\s*#{category}\s*(,|$)/i} unless c && c.any?
|
||||
# return closest match if there are multiple
|
||||
c = c.sort_by {|s| s.name.length}.first if c.respond_to? :sort_by
|
||||
c
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class GroupOrderArticle < ActiveRecord::Base
|
|||
logger.debug("Current quantity = #{self.quantity}, tolerance = #{self.tolerance}")
|
||||
|
||||
# When quantity and tolerance are zero, we don't serve any purpose
|
||||
if quantity == 0 and tolerance == 0
|
||||
if quantity == 0 && tolerance == 0
|
||||
logger.debug("Self-destructing since requested quantity and tolerance are zero")
|
||||
destroy!
|
||||
return
|
||||
|
|
@ -108,7 +108,7 @@ class GroupOrderArticle < ActiveRecord::Base
|
|||
# See description of the ordering algorithm in the general application documentation for details.
|
||||
def calculate_result(total = nil)
|
||||
# return memoized result unless a total is given
|
||||
return @calculate_result if total.nil? and not @calculate_result.nil?
|
||||
return @calculate_result if total.nil? && !@calculate_result.nil?
|
||||
|
||||
quantity = tolerance = total_quantity = 0
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ class Order < ActiveRecord::Base
|
|||
# but which have already been ordered in this stock order
|
||||
StockArticle.available.includes(:article_category).
|
||||
order('article_categories.name', 'articles.name').reject{ |a|
|
||||
a.quantity_available <= 0 and not a.ordered_in_order?(self)
|
||||
a.quantity_available <= 0 && !a.ordered_in_order?(self)
|
||||
}.group_by { |a| a.article_category.name }
|
||||
else
|
||||
supplier.articles.available.group_by { |a| a.article_category.name }
|
||||
|
|
@ -260,8 +260,8 @@ class Order < ActiveRecord::Base
|
|||
def keep_ordered_articles
|
||||
chosen_order_articles = order_articles.where(article_id: article_ids)
|
||||
to_be_removed = order_articles - chosen_order_articles
|
||||
to_be_removed_but_ordered = to_be_removed.select { |a| a.quantity > 0 or a.tolerance > 0 }
|
||||
unless to_be_removed_but_ordered.empty? or ignore_warnings
|
||||
to_be_removed_but_ordered = to_be_removed.select { |a| a.quantity > 0 || a.tolerance > 0 }
|
||||
unless to_be_removed_but_ordered.empty? || ignore_warnings
|
||||
errors.add(:articles, I18n.t(stockit? ? 'orders.model.warning_ordered_stock' : 'orders.model.warning_ordered'))
|
||||
@erroneous_article_ids = to_be_removed_but_ordered.map { |a| a.article_id }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -122,13 +122,13 @@ class OrderArticle < ActiveRecord::Base
|
|||
qty_left -= qty_for_members
|
||||
|
||||
# if there's anything left, move to stock if wanted
|
||||
if qty_left > 0 and surplus.index(:stock)
|
||||
if qty_left > 0 && surplus.index(:stock)
|
||||
counts[surplus.index(:stock)] = qty_left
|
||||
# 1) find existing stock article with same name, unit, price
|
||||
# 2) if not found, create new stock article
|
||||
# avoiding duplicate stock article names
|
||||
end
|
||||
if qty_left > 0 and surplus.index(nil)
|
||||
if qty_left > 0 && surplus.index(nil)
|
||||
counts[surplus.index(nil)] = qty_left
|
||||
end
|
||||
|
||||
|
|
@ -181,7 +181,7 @@ class OrderArticle < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def update_global_price=(value)
|
||||
@update_global_price = (value == true or value == '1') ? true : false
|
||||
@update_global_price = (value == true || value == '1') ? true : false
|
||||
end
|
||||
|
||||
# @return [Number] Units missing for the last +unit_quantity+ of the article.
|
||||
|
|
@ -207,7 +207,7 @@ class OrderArticle < ActiveRecord::Base
|
|||
|
||||
# Associate with current article price if created in a finished order
|
||||
def init_from_balancing
|
||||
if order.present? and order.finished?
|
||||
if order.present? && order.finished?
|
||||
self.article_price = article.article_prices.first
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -85,11 +85,11 @@ class Ordergroup < Group
|
|||
# The restriction can be deactivated for each ordergroup.
|
||||
# Only ordergroups, which have participated in more than 5 orders in total and more than 2 orders in apple time period
|
||||
def not_enough_apples?
|
||||
FoodsoftConfig[:use_apple_points] and
|
||||
FoodsoftConfig[:stop_ordering_under].present? and
|
||||
!ignore_apple_restriction and
|
||||
apples < FoodsoftConfig[:stop_ordering_under] and
|
||||
group_orders.count > 5 and
|
||||
FoodsoftConfig[:use_apple_points] &&
|
||||
FoodsoftConfig[:stop_ordering_under].present? &&
|
||||
!ignore_apple_restriction &&
|
||||
apples < FoodsoftConfig[:stop_ordering_under] &&
|
||||
group_orders.count > 5 &&
|
||||
group_orders.joins(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).count > 2
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class Supplier < ActiveRecord::Base
|
|||
|
||||
# try to convert different units
|
||||
new_price, new_unit_quantity = article.convert_units
|
||||
if new_price and new_unit_quantity
|
||||
if new_price && new_unit_quantity
|
||||
article.price = new_price
|
||||
article.unit_quantity = new_unit_quantity
|
||||
else
|
||||
|
|
@ -98,7 +98,7 @@ class Supplier < ActiveRecord::Base
|
|||
|
||||
# make sure the shared_sync_method is allowed for the shared supplier
|
||||
def valid_shared_sync_method
|
||||
if shared_supplier and !shared_supplier.shared_sync_methods.include?(shared_sync_method)
|
||||
if shared_supplier && !shared_supplier.shared_sync_methods.include?(shared_sync_method)
|
||||
errors.add :name, :included
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def self.authenticate(login, password)
|
||||
user = (find_by_nick(login) or find_by_email(login))
|
||||
user = find_by_nick(login) || find_by_email(login)
|
||||
if user && user.has_password(password)
|
||||
user
|
||||
else
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue