Skip to content

Instantly share code, notes, and snippets.

@surendra-wal
Last active May 3, 2017 13:10
Show Gist options
  • Save surendra-wal/4d2ed03cb3abd17dbc9fdb3247c3d65b to your computer and use it in GitHub Desktop.
Save surendra-wal/4d2ed03cb3abd17dbc9fdb3247c3d65b to your computer and use it in GitHub Desktop.
Spree code snippet

##Service to download a file from S3## ##Service to save a file to S3##

module Spree
  class AttachmentService
    attr_reader :content, :user
    attr_writer :success

    def initialize(content, key, user)
      @content = content
      @key = key
      @user = user
      success = false
    end
    
    def attach

      begin
        content.update_columns(status_message: 'Processing')
        filename = @key.split('/').last

        s3 = AWS::S3::Client.new

        File.open(filename, 'wb') do |file|
          s3.get_object(bucket_name: S3DirectUpload.config.bucket, key: @key) do |chunk|
            file.write(chunk)
          end
        end

        content.file = File.open(filename)
        content.user = user
        content.save
        s3 = AWS::S3.new
        s3.buckets[S3DirectUpload.config.bucket].objects[@key].delete
        success = true
      ensure
        File.delete(filename) if File.exist?(filename)
        content.update_columns(status_message: success ? 'Processed' : 'Failed')
        success ? Spree::ContentUploaderMailer.success(@user, @content).deliver_later : Spree::ContentUploaderMailer.failure(@user, @content).deliver_later
      end
    end

  end
end

##Controller for managing the content uploaded by admin##

module Spree
  module Admin
    class ContentsController < ResourceController

      belongs_to 'spree/product', find_by: :slug
      before_action :set_content_type, only: :create
      before_action :add_user_to_content, only: [:update, :destroy]

      def rebuild
        @content.rebuild_scrollable_tree(params[:prev_id].to_i, params[:next_id].to_i, params[:parent_id].to_i)
        render(nothing: true, status: :ok)
      end

      def add_zipfile
        @object.create_attachments(key, spree_current_user)
        redirect_to edit_admin_product_content_path(@product, @object), notice: Spree.t(:upload, scope: :content)
      end

      def upload
      end

      private

        def set_content_type
          if params[:content][:type].present?
            @object = @object.becomes(params[:content][:type].constantize)
          end
        end

        def key
          CGI.unescape(params[:url].split(/\//).last)
        end

        def add_user_to_content
          @content.user = spree_current_user
        end

        def load_content
          unless @content = Spree::Content.find_by(id: params[:id])
            render(nothing: true, status: :not_found)
          end
        end

        def location_after_save
          upload_admin_product_content_path(@product, @object)
        end

    end
  end
end

##Order model - Adding the extra behaviour for default spree order model##

Spree::Order.class_eval do

  include Spree::Order::CheckoutDecorator
  include ViewPresenter

  has_one :receipt, as: :receiptable
  validate :check_line_item_count

  before_save :build_receipt_if_completed, unless: :receipt

  MAX_LINE_ITEM_QUANTITY = 30

  def associate_user!(user, override_email = true)
    self.user           = user
    self.email          = user.email if override_email
    self.created_by   ||= user
    self.bill_address ||= user.bill_address
    self.ship_address ||= user.addresses.first

    changes = slice(:user_id, :email, :created_by_id, :bill_address_id, :ship_address_id)
    self.class.unscoped.where(id: self).update_all(changes)
  end

  private

    def build_receipt_if_completed
      if self.complete?
        self.create_receipt(user_id: user_id)
      end
    end

    def check_line_item_count
      if self.item_count > MAX_LINE_ITEM_QUANTITY
        errors.add(:base, Spree.t(:max_quantity_reached, value: MAX_LINE_ITEM_QUANTITY))
      end
    end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment