mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-14 23:47:48 +00:00
DEPRECATION WARNING: It looks like you are eager loading table(s) (one of: variant_overrides, enterprises, enterprise_roles) that are referenced in a string SQL snippet. For example:
Post.includes(:comments).where("comments.title = 'foo'")
Currently, Active Record recognizes the table in the string, and knows to JOIN the comments table to the query, rather than loading comments in a separate query. However, doing this without writing a full-blown SQL parser is inherently flawed. Since we don't want to write an SQL parser, we are removing this functionality. From now on, you must explicitly tell Active Record when you are referencing a table from a string:
Post.includes(:comments).where("comments.title = 'foo'").references(:comments)
If you don't rely on implicit join references you can disable the feature entirely by setting `config.active_record.disable_implicit_join_references = true`. (called from collection at /home/user/Github/openfoodnetwork/app/controllers/admin/variant_overrides_controller.rb:77)
97 lines
3.2 KiB
Ruby
97 lines
3.2 KiB
Ruby
require 'open_food_network/spree_api_key_loader'
|
|
|
|
module Admin
|
|
class VariantOverridesController < ResourceController
|
|
include OpenFoodNetwork::SpreeApiKeyLoader
|
|
include EnterprisesHelper
|
|
|
|
prepend_before_filter :load_data
|
|
before_filter :load_collection, only: [:bulk_update]
|
|
before_filter :load_spree_api_key, only: :index
|
|
|
|
def index; end
|
|
|
|
def bulk_update
|
|
# Ensure we're authorised to update all variant overrides
|
|
@vo_set.collection.each { |vo| authorize! :update, vo }
|
|
|
|
if @vo_set.save
|
|
# Return saved VOs with IDs
|
|
render json: @vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer
|
|
else
|
|
if @vo_set.errors.present?
|
|
render json: { errors: @vo_set.errors }, status: :bad_request
|
|
else
|
|
render nothing: true, status: :internal_server_error
|
|
end
|
|
end
|
|
end
|
|
|
|
def bulk_reset
|
|
# Ensure we're authorised to update all variant overrides.
|
|
@collection.each { |vo| authorize! :bulk_reset, vo }
|
|
@collection.each(&:reset_stock!)
|
|
|
|
if collection_errors.present?
|
|
render json: { errors: collection_errors }, status: :bad_request
|
|
else
|
|
render json: @collection, each_serializer: Api::Admin::VariantOverrideSerializer
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def load_data
|
|
@hubs = OpenFoodNetwork::Permissions.new(spree_current_user).
|
|
variant_override_hubs.by_name
|
|
|
|
# Used in JS to look up the name of the producer of each product
|
|
@producers = OpenFoodNetwork::Permissions.new(spree_current_user).
|
|
variant_override_producers
|
|
|
|
@hub_permissions = OpenFoodNetwork::Permissions.new(spree_current_user).
|
|
variant_override_enterprises_per_hub
|
|
|
|
@inventory_items = InventoryItem.where(enterprise_id: @hubs)
|
|
@import_dates = inventory_import_dates.uniq.to_json
|
|
end
|
|
|
|
def inventory_import_dates
|
|
import_dates = VariantOverride.
|
|
distinct_import_dates.
|
|
for_hubs(editable_enterprises.collect(&:id))
|
|
|
|
options = [{ id: '0', name: 'All' }]
|
|
import_dates.collect(&:import_date).map { |i| options.push(id: i.to_date, name: i.to_date.to_formatted_s(:long)) }
|
|
|
|
options
|
|
end
|
|
|
|
def load_collection
|
|
collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }]
|
|
@vo_set = VariantOverrideSet.new @variant_overrides, collection_attributes: collection_hash
|
|
end
|
|
|
|
def collection
|
|
@variant_overrides = VariantOverride.
|
|
includes(:variant).
|
|
for_hubs(params[:hub_id] || @hubs).
|
|
references(:variant)
|
|
@variant_overrides.select { |vo| vo.variant.present? }
|
|
end
|
|
|
|
def collection_actions
|
|
[:index, :bulk_update, :bulk_reset]
|
|
end
|
|
|
|
# This has been pulled from ModelSet as it is useful for compiling a list of errors on any generic collection (not necessarily a ModelSet)
|
|
# Could be pulled down into a lower level controller if it is useful in other high level controllers
|
|
def collection_errors
|
|
errors = ActiveModel::Errors.new self
|
|
full_messages = @collection.map { |element| element.errors.full_messages }.flatten
|
|
full_messages.each { |fm| errors.add(:base, fm) }
|
|
errors
|
|
end
|
|
end
|
|
end
|