migrate to Rails 4.0 (closes foodcoops#214)

Conflicts:
	Gemfile.lock
This commit is contained in:
wvengen 2014-02-20 15:04:53 +01:00
parent 12d1221bfc
commit 7841245795
97 changed files with 659 additions and 557 deletions

View file

@ -7,11 +7,11 @@ class Article < ActiveRecord::Base
# Associations
belongs_to :supplier
belongs_to :article_category
has_many :article_prices, :order => "created_at DESC"
has_many :article_prices, -> { order("created_at DESC") }
scope :undeleted, -> { where(deleted_at: nil) }
scope :available, -> { undeleted.where(availability: true) }
scope :not_in_stock, :conditions => {:type => nil}
scope :not_in_stock, -> { where(type: nil) }
# Validations
validates_presence_of :name, :unit, :price, :tax, :deposit, :unit_quantity, :supplier_id, :article_category
@ -44,7 +44,7 @@ class Article < ActiveRecord::Base
# If the article is used in an open Order, the Order will be returned.
def in_open_order
@in_open_order ||= begin
order_articles = OrderArticle.all(:conditions => ['order_id IN (?)', Order.open.collect(&:id)])
order_articles = OrderArticle.where(order_id: Order.open.collect(&:id))
order_article = order_articles.detect {|oa| oa.article_id == id }
order_article ? order_article.order : nil
end

View file

@ -2,12 +2,9 @@ class Delivery < ActiveRecord::Base
belongs_to :supplier
has_one :invoice
has_many :stock_changes,
:dependent => :destroy,
:include => 'stock_article',
:order => 'articles.name ASC'
has_many :stock_changes, -> { includes(:stock_article).order('articles.name ASC') }, :dependent => :destroy
scope :recent, :order => 'created_at DESC', :limit => 10
scope :recent, -> { order('created_at DESC').limit(10) }
validates_presence_of :supplier_id, :delivered_on
validate :stock_articles_must_be_unique

View file

@ -17,7 +17,7 @@ class Group < ActiveRecord::Base
# Returns all NONmembers and a checks for possible multiple Ordergroup-Memberships
def non_members
User.natural_order.all.reject { |u| users.include?(u) }
User.natural_order.reject { |u| users.include?(u) }
end
def user_tokens=(ids)

View file

@ -14,10 +14,10 @@ class GroupOrder < ActiveRecord::Base
validates_numericality_of :price
validates_uniqueness_of :ordergroup_id, :scope => :order_id # order groups can only order once per order
scope :in_open_orders, joins(:order).merge(Order.open)
scope :in_finished_orders, joins(:order).merge(Order.finished_not_closed)
scope :in_open_orders, -> { joins(:order).merge(Order.open) }
scope :in_finished_orders, -> { joins(:order).merge(Order.finished_not_closed) }
scope :ordered, :include => :ordergroup, :order => 'groups.name'
scope :ordered, -> { includes(:ordergroup).order('groups.name') }
# Generate some data for the javascript methods in ordering view
def load_data
@ -55,7 +55,7 @@ class GroupOrder < ActiveRecord::Base
def save_group_order_articles
for order_article in order.order_articles
# Find the group_order_article, create a new one if necessary...
group_order_article = group_order_articles.find_or_create_by_order_article_id(order_article.id)
group_order_article = group_order_articles.where(order_article_id: order_article.id).first_or_create
# Get ordered quantities and update group_order_articles/_quantities...
quantities = group_order_articles_attributes.fetch(order_article.id.to_s, {:quantity => 0, :tolerance => 0})

View file

@ -13,18 +13,18 @@ class GroupOrderArticle < ActiveRecord::Base
validates_inclusion_of :tolerance, :in => 0..99
validates_uniqueness_of :order_article_id, :scope => :group_order_id # just once an article per group order
scope :ordered, :conditions => 'group_order_articles.result > 0 OR group_order_articles.quantity > 0 OR group_order_articles.tolerance > 0', :include => {:group_order => :ordergroup}, :order => 'groups.name'
scope :ordered, -> { includes(:group_order => :ordergroup).where('group_order_articles.result > 0 OR group_order_articles.quantity > 0 OR group_order_articles.tolerance > 0').order('groups.name') }
localize_input_of :result
# Setter used in group_order_article#new
# We have to create an group_order, if the ordergroup wasn't involved in the order yet
def ordergroup_id=(id)
self.group_order = GroupOrder.find_or_initialize_by_order_id_and_ordergroup_id(order_article.order_id, id)
self.group_order = GroupOrder.where(order_id: order_article.order_id, ordergroup_id: id).first_or_initialize
end
def ordergroup_id
group_order.try(:ordergroup_id)
group_order.try!(:ordergroup_id)
end
# Updates the quantity/tolerance for this GroupOrderArticle by updating both GroupOrderArticle properties
@ -36,7 +36,7 @@ class GroupOrderArticle < ActiveRecord::Base
logger.debug("Current quantity = #{self.quantity}, tolerance = #{self.tolerance}")
# Get quantities ordered with the newest item first.
quantities = group_order_article_quantities.find(:all, :order => 'created_on desc')
quantities = group_order_article_quantities.order('created_on DESC').to_a
logger.debug("GroupOrderArticleQuantity items found: #{quantities.size}")
if (quantities.size == 0)
@ -120,8 +120,7 @@ class GroupOrderArticle < ActiveRecord::Base
# In total there are enough units ordered. Now check the individual result for the ordergroup (group_order).
#
# Get all GroupOrderArticleQuantities for this OrderArticle...
order_quantities = GroupOrderArticleQuantity.all(
:conditions => ["group_order_article_id IN (?)", order_article.group_order_article_ids], :order => 'created_on')
order_quantities = GroupOrderArticleQuantity.where(group_order_article_id: order_article.group_order_article_ids).order('created_on')
logger.debug "GroupOrderArticleQuantity records found: #{order_quantities.size}"
# Determine quantities to be ordered...

View file

@ -5,7 +5,7 @@ class Invite < ActiveRecord::Base
belongs_to :user
belongs_to :group
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
validates_presence_of :user
validates_presence_of :group
validates_presence_of :token

View file

@ -7,7 +7,7 @@ class Invoice < ActiveRecord::Base
validates_presence_of :supplier_id
validates_numericality_of :amount, :deposit, :deposit_credit
scope :unpaid, :conditions => { :paid_on => nil }
scope :unpaid, -> { where(paid_on: nil) }
# Replace numeric seperator with database format
localize_input_of :amount, :deposit, :deposit_credit

View file

@ -5,14 +5,12 @@ class Membership < ActiveRecord::Base
before_destroy :check_last_admin
# messages
ERR_NO_ADMIN_MEMBER_DELETE = I18n.t('model.membership.no_admin_delete')
protected
# check if this is the last admin-membership and deny
def check_last_admin
raise ERR_NO_ADMIN_MEMBER_DELETE if self.group.role_admin? && self.group.memberships.size == 1 && Group.find_all_by_role_admin(true).size == 1
raise I18n.t('model.membership.no_admin_delete') if self.group.role_admin? && self.group.memberships.size == 1 && Group.where(role_admin: true).count == 1
end
end

View file

@ -4,9 +4,9 @@ class Message < ActiveRecord::Base
serialize :recipients_ids, Array
attr_accessor :sent_to_all, :group_id, :recipient_tokens, :reply_to
scope :pending, where(:email_state => 0)
scope :sent, where(:email_state => 1)
scope :public, where(:private => false)
scope :pending, -> { where(:email_state => 0) }
scope :sent, -> { where(:email_state => 1) }
scope :public, -> { where(:private => false) }
# Values for the email_state attribute: :none, :pending, :sent, :failed
EMAIL_STATE = {

View file

@ -10,7 +10,7 @@ class Order < ActiveRecord::Base
has_many :group_orders, :dependent => :destroy
has_many :ordergroups, :through => :group_orders
has_one :invoice
has_many :comments, :class_name => "OrderComment", :order => "created_at"
has_many :comments, -> { order('created_at') }, :class_name => "OrderComment"
has_many :stock_changes
belongs_to :supplier
belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_user_id'
@ -25,11 +25,11 @@ class Order < ActiveRecord::Base
after_save :save_order_articles, :update_price_of_group_orders
# Finders
scope :open, where(state: 'open').order('ends DESC')
scope :finished, where("orders.state = 'finished' OR orders.state = 'closed'").order('ends DESC')
scope :finished_not_closed, where(state: 'finished').order('ends DESC')
scope :closed, where(state: 'closed').order('ends DESC')
scope :stockit, where(supplier_id: 0).order('ends DESC')
scope :open, -> { where(state: 'open').order('ends DESC') }
scope :finished, -> { where("orders.state = 'finished' OR orders.state = 'closed'").order('ends DESC') }
scope :finished_not_closed, -> { where(state: 'finished').order('ends DESC') }
scope :closed, -> { where(state: 'closed').order('ends DESC') }
scope :stockit, -> { where(supplier_id: 0).order('ends DESC') }
def stockit?
supplier_id == 0
@ -43,12 +43,12 @@ class Order < ActiveRecord::Base
if stockit?
# make sure to include those articles which are no longer available
# but which have already been ordered in this stock order
StockArticle.available.all(:include => :article_category,
:order => 'article_categories.name, articles.name').reject{ |a|
StockArticle.available.includes(:article_category).
order('article_categories.name, articles.name').reject{ |a|
a.quantity_available <= 0 and not a.ordered_in_order?(self)
}.group_by { |a| a.article_category.name }
else
supplier.articles.available.all.group_by { |a| a.article_category.name }
supplier.articles.available.group_by { |a| a.article_category.name }
end
end
@ -107,7 +107,7 @@ class Order < ActiveRecord::Base
end
def articles_sort_by_category
order_articles.all(:include => [:article], :order => 'articles.name').sort do |a,b|
order_articles.includes(:article).order('articles.name').sort do |a,b|
a.article.article_category.name <=> b.article.article_category.name
end
end
@ -168,7 +168,7 @@ class Order < ActiveRecord::Base
# Update order_articles. Save the current article_price to keep price consistency
# Also save results for each group_order_result
# Clean up
order_articles.all(:include => :article).each do |oa|
order_articles.includes(:article).each do |oa|
oa.update_attribute(:article_price, oa.article.article_prices.first)
oa.group_order_articles.each do |goa|
goa.save_results!
@ -199,7 +199,7 @@ class Order < ActiveRecord::Base
transaction_note = I18n.t('orders.model.notice_close', :name => name,
:ends => ends.strftime(I18n.t('date.formats.default')))
gos = group_orders.all(:include => :ordergroup) # Fetch group_orders
gos = group_orders.includes(:ordergroup) # Fetch group_orders
gos.each { |group_order| group_order.update_price! } # Update prices of group_orders
transaction do # Start updating account balances
@ -209,7 +209,7 @@ class Order < ActiveRecord::Base
end
if stockit? # Decreases the quantity of stock_articles
for oa in order_articles.all(:include => :article)
for oa in order_articles.includes(:article)
oa.update_results! # Update units_to_order of order_article
stock_changes.create! :stock_article => oa.article, :quantity => oa.units_to_order*-1
end
@ -236,7 +236,7 @@ class Order < ActiveRecord::Base
end
def keep_ordered_articles
chosen_order_articles = order_articles.find_all_by_article_id(article_ids)
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

View file

@ -19,17 +19,6 @@ class OrderArticle < ActiveRecord::Base
before_create :init_from_balancing
after_destroy :update_ordergroup_prices
def self.sort_by_name(order_articles)
order_articles.sort { |a,b| a.article.name <=> b.article.name }
end
def self.sort_by_order_number(order_articles)
order_articles.sort do |a,b|
a.article.order_number.to_s.gsub(/[^[:digit:]]/, "").to_i <=>
b.article.order_number.to_s.gsub(/[^[:digit:]]/, "").to_i
end
end
# This method returns either the ArticlePrice or the Article
# The first will be set, when the the order is finished
def price

View file

@ -62,9 +62,9 @@ class Ordergroup < Group
def update_stats!
# Get hours for every job of each user in period
jobs = users.sum { |u| u.tasks.done.sum(:duration, :conditions => ["updated_on > ?", APPLE_MONTH_AGO.month.ago]) }
jobs = users.to_a.sum { |u| u.tasks.done.where('updated_on > ?', APPLE_MONTH_AGO.month.ago).sum(:duration) }
# Get group_order.price for every finished order in this period
orders_sum = group_orders.includes(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).sum(:price)
orders_sum = group_orders.includes(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).references(:orders).sum(:price)
@readonly = false # Dirty hack, avoid getting RecordReadOnly exception when called in task after_save callback. A rails bug?
update_attribute(:stats, {:jobs_size => jobs, :orders_sum => orders_sum})
@ -113,8 +113,8 @@ class Ordergroup < Group
# Make sure, the name is uniq, add usefull message if uniq group is already deleted
def uniqueness_of_name
group = Ordergroup.where('groups.name = ?', name)
group = group.where('groups.id != ?', self.id) unless new_record?
group = Ordergroup.where(name: name)
group = group.where.not(id: self.id) unless new_record?
if group.exists?
message = group.first.deleted? ? :taken_with_deleted : :taken
errors.add :name, message

View file

@ -3,7 +3,7 @@ class StockArticle < Article
has_many :stock_changes
scope :available, -> { undeleted.where'quantity > 0' }
scope :available, -> { undeleted.where('quantity > 0') }
before_destroy :check_quantity
@ -19,7 +19,7 @@ class StockArticle < Article
def quantity_ordered
OrderArticle.where(article_id: id).
joins(:order).where("orders.state = 'open' OR orders.state = 'finished'").sum(:units_to_order)
joins(:order).where(orders: {state: ['open', 'finished']}).sum(:units_to_order)
end
def quantity_history

View file

@ -1,14 +1,13 @@
# encoding: utf-8
class Supplier < ActiveRecord::Base
has_many :articles, :conditions => {:type => nil},
:include => [:article_category], :order => 'article_categories.name, articles.name'
has_many :stock_articles, :include => [:article_category], :order => 'article_categories.name, articles.name'
has_many :articles, -> { where(:type => nil).includes(:article_category).order('article_categories.name', 'articles.name') }
has_many :stock_articles, -> { includes(:article_category).order('article_categories.name', 'articles.name') }
has_many :orders
has_many :deliveries
has_many :invoices
belongs_to :shared_supplier # for the sharedLists-App
include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
:delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity
@ -82,8 +81,8 @@ class Supplier < ActiveRecord::Base
# Make sure, the name is uniq, add usefull message if uniq group is already deleted
def uniqueness_of_name
supplier = Supplier.where('suppliers.name = ?', name)
supplier = supplier.where('suppliers.id != ?', self.id) unless new_record?
supplier = Supplier.where(name: name)
supplier = supplier.where.not(id: self.id) unless new_record?
if supplier.exists?
message = supplier.first.deleted? ? :taken_with_deleted : :taken
errors.add :name, message

View file

@ -5,13 +5,14 @@ class Task < ActiveRecord::Base
belongs_to :workgroup
belongs_to :periodic_task_group
scope :non_group, where(workgroup_id: nil, done: false)
scope :done, where(done: true)
scope :undone, where(done: false)
scope :non_group, -> { where(workgroup_id: nil, done: false) }
scope :done, -> { where(done: true) }
scope :undone, -> { where(done: false) }
attr_accessor :current_user_id
# form will send user in string. responsibilities will added later
include ActiveModel::MassAssignmentSecurity
attr_protected :users
validates :name, :presence => true, :length => { :minimum => 3 }

View file

@ -26,7 +26,7 @@ class User < ActiveRecord::Base
validates_presence_of :email
validates_presence_of :password, :on => :create
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
validates_uniqueness_of :email, :case_sensitive => false
validates_presence_of :first_name # for simple_form validations
validates_length_of :first_name, :in => 2..50
@ -157,7 +157,7 @@ class User < ActiveRecord::Base
#Returns an array with the users groups (but without the Ordergroups -> because tpye=>"")
def member_of_groups()
self.groups.find(:all, :conditions => {:type => ""})
self.groups.where(type: '')
end
def self.authenticate(login, password)

View file

@ -3,7 +3,7 @@ class Workgroup < Group
has_many :tasks
# returns all non-finished tasks
has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], order: 'due_date ASC, name ASC'
has_many :open_tasks, -> { where(:done => false).order('due_date ASC, name ASC') }, :class_name => 'Task'
validates_uniqueness_of :name
validate :last_admin_on_earth, :on => :update
@ -13,7 +13,7 @@ class Workgroup < Group
# Check before destroy a group, if this is the last group with admin role
def check_last_admin_group
if role_admin && Workgroup.where(:role_admin => true).size == 1
if role_admin && Workgroup.where(role_admin: true).size == 1
raise I18n.t('workgroups.error_last_admin_group')
end
end
@ -21,7 +21,7 @@ class Workgroup < Group
# add validation check on update
# Return an error if this is the last group with admin role and role_admin should set to false
def last_admin_on_earth
if !role_admin && !Workgroup.where('role_admin = ? AND id != ?', true, id).exists?
if !role_admin && !Workgroup.where(role_admin: true).where.not(id: id).exists?
errors.add(:role_admin, I18n.t('workgroups.error_last_admin_role'))
end
end