Sha256: 355085458f88248259116032fb14e0762c51bf0dcdef5b5e1bb6449e6ad4359e

Contents?: true

Size: 1.99 KB

Versions: 62

Compression:

Stored size: 1.99 KB

Contents

module Workarea
  class Admin::ShippingServicesController < Admin::ApplicationController
    required_permissions :settings
    before_action :find_service, except: :index

    def index
      @services = Shipping::Service.all.page(params[:page])
    end

    def show
      redirect_to edit_shipping_service_path(@service)
    end

    def new; end

    def edit; end

    def create
      build_rules

      if @service.save

        flash[:success] = t('workarea.admin.shipping_services.flash_messages.created')
        redirect_to shipping_services_path
      else
        render :new
      end
    end

    def update
      build_rules

      if @service.update_attributes(params[:service])
        flash[:success] = t('workarea.admin.shipping_services.flash_messages.saved')
        redirect_to shipping_services_path
      else
        render :edit
      end
    end

    def destroy
      @service.destroy
      flash[:success] = t('workarea.admin.shipping_services.flash_messages.removed')
      redirect_to shipping_services_path
    end

    private

    def find_service
      if params[:id].present?
        @service = Shipping::Service.find(params[:id])
      else
        @service = Shipping::Service.new(params[:service])
      end
    end

    def build_rules
      #this updates existing rates
      if params[:rates].present?
        params[:rates].each do |rate, attrs|
          if attrs[:price].present?
            attrs.each { |k,v| attrs[k] = nil if v.blank? }
            @service.rates.find(rate).update_attributes(attrs)
          end
        end
      end

      if params[:rates_to_remove].present?
        params[:rates_to_remove].each do |id|
          @service.rates.find(id).destroy
        end
      end

      #this adds new rates
      if params[:new_rates].present?
        Array(params[:new_rates]).flatten.each do |rate|
          if rate[:price].present?
            rate.delete_if { |k, v| v.blank? }
            @service.rates.build(rate)
          end
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-admin-3.5.27 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.26 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.45 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.25 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.23 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.44 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.22 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.43 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.21 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.42 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.20 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.41 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.19 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.40 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.18 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.39 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.17 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.38 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.5.16 app/controllers/workarea/admin/shipping_services_controller.rb
workarea-admin-3.4.37 app/controllers/workarea/admin/shipping_services_controller.rb