Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vector < ActiveRecord::Base
- PER_PAGE = 49
- include Rails.application.routes.url_helpers
- paginates_per PER_PAGE
- extend FriendlyId
- attr_accessor :zip, :category_tokens, :tag_tokens
- attr_accessible :zip, :name, :category_tokens, :tag_tokens, :standart_price, :extended_price,
- :description, :popularity, :primary_category_id, :secondary_category_id,
- :for_free, :standart_price_cents, :extended_price_cents
- image_accessor :image
- image_accessor :preview_image
- image_accessor :thumb_image
- image_accessor :zoom_image
- image_accessor :main_image
- file_accessor :vector
- validates_presence_of :vector, :image, :name, :standart_price, :extended_price
- validates_numericality_of :standart_price, :extended_price
- validates_presence_of :primary_category_id, :on => :update
- has_and_belongs_to_many :tags
- has_many :line_items, dependent: :destroy
- has_many :lightbox_items, dependent: :destroy
- belongs_to :primary_category, :class_name => Category
- belongs_to :secondary_category, :class_name => Category
- default_scope where(for_free: false)
- scope :created, lambda { recent }
- scope :popular, order('popularity DESC')
- scope :recent, order('created_at DESC')
- scope :free, where(for_free: true)
- monetize :standart_price_cents
- monetize :extended_price_cents
- friendly_id :name, use: :slugged
- before_validation :extract_zip, :if => Proc.new { |model| model.zip }
- before_create :store_orientation
- after_save :push_jobs
- define_index do
- indexes :name
- indexes :description
- indexes tags(:name), :as => :tag
- indexes [primary_category.name, secondary_category.name], :as => :category
- has :popularity
- has :created_at
- set_property :field_weights => {
- :name => 100,
- :description => 90,
- :category => 80,
- :tag => 70
- }
- set_property :star => true
- end
- def to_jq_upload
- {
- 'name' => read_attribute(:name),
- 'size' => image.size,
- 'url' => thumb.url,
- 'delete_url' => 'gg',
- 'delete_type' => 'DELETE'
- }
- end
- def normalize_friendly_id(string)
- super.gsub("_", "-")
- end
- def tag_tokens=(val)
- self.tags = val.split(',').map {|name| name.gsub(/\s+/, '').downcase }.uniq.map{|name|Tag.find_or_create_by_name(name) }
- end
- def tag_tokens
- tags.map(&:name).join(', ')
- end
- def preview
- make_thumb(:w => 300, :h => 315)
- end
- def thumb
- make_thumb(:w => 160, :h => 168)
- end
- def zoom
- make_thumb(:w => 769, :h => 800, :watermark => __method__.to_sym)
- end
- def main
- make_thumb(:w => 380, :h => 400, :watermark => __method__.to_sym)
- end
- def popularity
- super || 0
- end
- def humanized_name
- name.gsub(/_/,' ')
- end
- def similar_vectors
- tv = Arel::Table.new(:tags_vectors)
- v = Vector.arel_table
- t = Tag.arel_table
- Vector.where(v[:id].in(
- tv.project(tv[:vector_id])
- .where(tv[:tag_id].in(
- tv.project(tv[:tag_id])
- .where(tv[:vector_id].eq(id)))
- .and( tv[:vector_id].not_eq(id)))
- .having(tv[:tag_id].count.gt(9))
- .group(tv[:vector_id])))
- end
- def self.full_search(q, params = {})
- search(q,
- :order => params[:order] || :created_at,
- :sort_mode => params[:sort_mode] || :desc
- )
- end
- private
- def make_thumb(attrs = {})
- extent = "#{attrs[:w]}x#{attrs[:h]}"
- color = "#ebeaea"
- if is_landscape
- job = image.process(:convert, "-resize #{attrs[:w]}x -background \"#{color}\" -gravity center -extent #{extent}")
- else
- job = image.process(:convert, "-resize x#{attrs[:h]} -background \"#{color}\" -gravity center -extent #{extent}")
- end
- attrs[:watermark] ? job.process(:watermark, attrs[:watermark]) : job
- end
- def extract_zip
- raise "Not An Uploaded File" unless zip.is_a? ActionDispatch::Http::UploadedFile
- ::Zip::Archive.open(zip.tempfile.to_path.to_s) do |ar|
- raise "Only two files supported" unless ar.num_files == 2
- self.name = name_from_tempfile
- 2.times do |i|
- entry_name = ar.get_name(i)
- ar.fopen(entry_name) do |f|
- string = f.read
- string.class.send(:define_singleton_method, :original_filename) {f.name}
- case entry_name
- when /.*\.eps\Z/
- self.vector = string
- when /.*\.jpg\Z/i
- self.image = string
- else
- raise "Wrong file in Acrhive. Only EPS and JPG should persist"
- end
- end
- end
- end
- end
- def name_from_tempfile
- File.basename(zip.original_filename, '.*')
- end
- def store_orientation
- self.is_landscape = self.image.landscape?
- end
- def push_jobs
- [:main, :thumb, :preview, :zoom].each do |variant|
- Delayed::Job.enqueue Omg::VectorJob.new(self, variant)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement