Merge master into producer-emails.

This commit is contained in:
Paul Mackay
2015-03-04 19:09:43 +00:00
468 changed files with 11434 additions and 3239 deletions

View File

@@ -1,22 +1,50 @@
module Admin
class EnterpriseGroupsController < ResourceController
before_filter :load_data, except: :index
before_filter :load_object_data, only: [:new, :edit, :create, :update]
def index
@enterprise_groups = @enterprise_groups.managed_by(spree_current_user)
end
def move_up
@enterprise_group = EnterpriseGroup.find params[:enterprise_group_id]
@enterprise_group.move_higher
EnterpriseGroup.with_isolation_level_serializable do
@enterprise_group = EnterpriseGroup.find params[:enterprise_group_id]
@enterprise_group.move_higher
end
redirect_to main_app.admin_enterprise_groups_path
end
def move_down
@enterprise_group = EnterpriseGroup.find params[:enterprise_group_id]
@enterprise_group.move_lower
EnterpriseGroup.with_isolation_level_serializable do
@enterprise_group = EnterpriseGroup.find params[:enterprise_group_id]
@enterprise_group.move_lower
end
redirect_to main_app.admin_enterprise_groups_path
end
protected
def build_resource_with_address
enterprise_group = build_resource_without_address
enterprise_group.address = Spree::Address.new
enterprise_group.address.country = Spree::Country.find_by_id(Spree::Config[:default_country_id])
enterprise_group
end
alias_method_chain :build_resource, :address
private
def load_data
@countries = Spree::Country.order(:name)
@enterprises = Enterprise.activated
end
def load_object_data
@owner_email = @enterprise_group.andand.owner.andand.email || ""
end
def collection
EnterpriseGroup.by_position
end

View File

@@ -1,13 +1,15 @@
module Admin
class EnterprisesController < ResourceController
before_filter :load_enterprise_set, :only => :index
before_filter :load_countries, :except => :index
before_filter :load_countries, :except => [:index, :set_sells, :check_permalink]
before_filter :load_methods_and_fees, :only => [:new, :edit, :update, :create]
before_filter :load_taxons, :only => [:new, :edit, :update, :create]
before_filter :check_can_change_sells, only: :update
before_filter :check_can_change_bulk_sells, only: :bulk_update
before_filter :override_owner, only: :create
before_filter :check_can_change_owner, only: :update
before_filter :check_can_change_bulk_owner, only: :bulk_update
before_filter :check_can_change_managers, only: :update
helper 'spree/products'
include OrderCyclesHelper
@@ -16,18 +18,44 @@ module Admin
@collection = order_cycle_permitted_enterprises
end
def set_sells
enterprise = Enterprise.find_by_permalink(params[:id]) || Enterprise.find(params[:id])
attributes = { sells: params[:sells] }
attributes[:producer_profile_only] = params[:sells] == "none" && !!params[:producer_profile_only]
attributes[:shop_trial_start_date] = Time.now if params[:sells] == "own"
if %w(none own).include?(params[:sells])
if params[:sells] == 'own' && enterprise.shop_trial_start_date
expiry = enterprise.shop_trial_start_date + Enterprise::SHOP_TRIAL_LENGTH.days
if Time.now > expiry
flash[:error] = "Sorry, but you've already had a trial. Expired on: #{expiry.strftime('%Y-%m-%d')}"
else
attributes.delete :shop_trial_start_date
enterprise.update_attributes(attributes)
flash[:notice] = "Welcome back! Your trial expires on: #{expiry.strftime('%Y-%m-%d')}"
end
elsif enterprise.update_attributes(attributes)
flash[:success] = "Congratulations! Registration for #{enterprise.name} is complete!"
end
else
flash[:error] = "Unauthorised"
end
redirect_to admin_path
end
def bulk_update
@enterprise_set = EnterpriseSet.new(params[:enterprise_set])
@enterprise_set = EnterpriseSet.new(collection, params[:enterprise_set])
if @enterprise_set.save
flash[:success] = 'Enterprises updated successfully'
redirect_to main_app.admin_enterprises_path
else
touched_ids = params[:enterprise_set][:collection_attributes].values.map { |v| v[:id].to_i }
@enterprise_set.collection.select! { |e| touched_ids.include? e.id }
flash[:error] = 'Update failed'
render :index
end
end
protected
def build_resource_with_address
@@ -38,11 +66,16 @@ module Admin
end
alias_method_chain :build_resource, :address
# Overriding method on Spree's resource controller,
# so that resources are found using permalink
def find_resource
Enterprise.find_by_permalink(params[:id])
end
private
def load_enterprise_set
@enterprise_set = EnterpriseSet.new :collection => collection
@enterprise_set = EnterpriseSet.new collection
end
def load_countries
@@ -50,8 +83,10 @@ module Admin
end
def collection
# TODO was ordered with is_distributor DESC as well, not sure why or how we want ot sort this now
Enterprise.managed_by(spree_current_user).order('is_primary_producer ASC, name')
# TODO was ordered with is_distributor DESC as well, not sure why or how we want to sort this now
OpenFoodNetwork::Permissions.new(spree_current_user).
editable_enterprises.
order('is_primary_producer ASC, name')
end
def collection_actions
@@ -64,6 +99,10 @@ module Admin
@enterprise_fees = EnterpriseFee.managed_by(spree_current_user).for_enterprise(@enterprise).order(:fee_type, :name).all
end
def load_taxons
@taxons = Spree::Taxon.order(:name)
end
def check_can_change_bulk_sells
unless spree_current_user.admin?
params[:enterprise_set][:collection_attributes].each do |i, enterprise_params|
@@ -94,6 +133,12 @@ module Admin
end
end
def check_can_change_managers
unless ( spree_current_user == @enterprise.owner ) || spree_current_user.admin?
params[:enterprise].delete :user_ids
end
end
# Overriding method on Spree's resource controller
def location_after_save
if params[:enterprise].key? :producer_properties_attributes

View File

@@ -12,7 +12,7 @@ module Admin
end
def load_enterprise
@enterprise = Enterprise.find params[:enterprise_id]
@enterprise = Enterprise.find_by_permalink params[:enterprise_id]
end
def load_properties

View File

@@ -0,0 +1,46 @@
require 'open_food_network/spree_api_key_loader'
module Admin
class VariantOverridesController < ResourceController
include OrderCyclesHelper
include OpenFoodNetwork::SpreeApiKeyLoader
before_filter :load_spree_api_key, only: :index
def index
@hubs = order_cycle_hub_enterprises(without_validation: true)
# 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
@variant_overrides = VariantOverride.for_hubs(@hubs)
end
def bulk_update
collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }]
vo_set = VariantOverrideSet.new collection_attributes: collection_hash
# 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: 400
else
render nothing: true, status: 500
end
end
end
def collection
end
end
end