lib/uploader/asset.rb in rails-uploader-0.2.8 vs lib/uploader/asset.rb in rails-uploader-0.3.0
- old
+ new
@@ -1,104 +1,83 @@
+require 'active_support/concern'
+
module Uploader
module Asset
- def self.included(base)
- base.send(:extend, Uploader::Asset::ClassMethods)
- base.send(:include, Uploader::Asset::AssetProcessor)
+ extend ActiveSupport::Concern
- base.instance_eval do
- before_create :generate_public_token
+ module ClassMethods
+ def fileupload_find_asset(params)
+ where(id: params[:id]).first
end
- end
- module Mongoid
- def self.included(base)
- base.send(:extend, Uploader::Asset::ClassMethods)
- base.send(:include, Uploader::Asset::AssetProcessor)
-
- base.instance_eval do
- field :guid, type: String
- field :public_token, type: String
-
- before_create :generate_public_token
- end
+ def fileupload_find_assets(params)
+ where(assetable_type: params[:assetable_type], assetable_id: params[:assetable_id])
end
- def as_json(options = {})
- json_data = super
- json_data['filename'] = File.basename(data.path)
- json_data['size'] = data.file.size
- json_data['id'] = json_data['_id']
- json_data['thumb_url'] = data.thumb.url if data.respond_to?(:thumb)
+ def fileupload_update_ordering(params)
+ assets = Array.wrap(params[:assets] || [])
- json_data
- end
-
- def assetable_id_format(assetable_id)
- Moped::BSON::ObjectId.from_string(assetable_id)
- end
-
- class << self
- def include_root_in_json
- false
+ assets.each_with_index do |id, index|
+ where(id: id).update_all(sort_order: index)
end
end
end
- module ClassMethods
- def generate_token(column)
- loop do
- token = Uploader.guid
- break token unless where({ column => token }).exists?
- end
- end
+ # Save asset
+ # Usage:
+ #
+ # class Asset < ActiveRecord::Base
+ # include Uploader::Asset
+ #
+ # def fileupload_create(params, request = nil)
+ # self.user = request.env['warden'].user
+ # super
+ # end
+ # end
+ #
+ def fileupload_create(params, _request = nil)
+ self[Uploader.guid_column] = params[:guid]
+ fileupload_set_assetable(params)
+ save
end
- module AssetProcessor
- # Save asset
- # Usage:
- #
- # class Asset < ActiveRecord::Base
- # include Uploader::Asset
- #
- # def uploader_create(params, request = nil)
- # self.user = request.env['warden'].user
- # super
- # end
- # end
- #
- def uploader_create(params, request = nil)
- self.guid = params[:guid]
- self.assetable_type = params[:assetable_type]
- self.assetable_id = assetable_id_format(params[:assetable_id])
- save
- end
+ # Destroy asset
+ # Usage (cancan example):
+ #
+ # class Asset < ActiveRecord::Base
+ # include Uploader::Asset
+ #
+ # def fileupload_destroy(params, request = nil)
+ # ability = Ability.new(request.env['warden'].user)
+ # if ability.can? :delete, self
+ # super
+ # else
+ # errors.add(:id, :access_denied)
+ # end
+ # end
+ # end
+ #
+ def fileupload_destroy(_params, _request = nil)
+ destroy
+ end
- # Destroy asset
- # Usage (cancan example):
- #
- # class Asset < ActiveRecord::Base
- # include Uploader::Asset
- #
- # def uploader_destroy(params, request = nil)
- # ability = Ability.new(request.env['warden'].user)
- # if ability.can? :delete, self
- # super
- # else
- # errors.add(:id, :access_denied)
- # end
- # end
- # end
- #
- def uploader_destroy(params, request)
- destroy
- end
-
- def generate_public_token
- self.public_token = self.class.generate_token(:public_token)
- end
+ # Serialize asset to fileupload JSON format
+ #
+ def to_fileupload
+ {
+ id: id,
+ name: filename,
+ content_type: content_type,
+ size: size,
+ url: url,
+ thumb_url: thumb_url
+ }
end
- def assetable_id_format(assetable_id)
- assetable_id || 0
+ protected
+
+ def fileupload_set_assetable(params)
+ self.assetable_type = params[:assetable_type]
+ self.assetable_id = params[:assetable_id]
end
end
end