Advertisement
twinfacer

Model Code

Jun 28th, 2013
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.92 KB | None | 0 0
  1. class Vector < ActiveRecord::Base
  2.   PER_PAGE = 49
  3.   include Rails.application.routes.url_helpers
  4.   paginates_per PER_PAGE
  5.   extend FriendlyId
  6.   attr_accessor :zip, :category_tokens, :tag_tokens
  7.   attr_accessible :zip, :name, :category_tokens, :tag_tokens, :standart_price, :extended_price,
  8.                   :description, :popularity, :primary_category_id, :secondary_category_id,
  9.                   :for_free, :standart_price_cents, :extended_price_cents
  10.   image_accessor :image
  11.   image_accessor :preview_image
  12.   image_accessor :thumb_image
  13.   image_accessor :zoom_image
  14.   image_accessor :main_image
  15.   file_accessor :vector
  16.  
  17.   validates_presence_of :vector, :image, :name, :standart_price, :extended_price
  18.   validates_numericality_of :standart_price, :extended_price
  19.   validates_presence_of :primary_category_id, :on => :update
  20.  
  21.   has_and_belongs_to_many :tags
  22.   has_many :line_items, dependent: :destroy
  23.   has_many :lightbox_items, dependent: :destroy
  24.   belongs_to :primary_category, :class_name => Category
  25.   belongs_to :secondary_category, :class_name => Category
  26.  
  27.   default_scope where(for_free: false)
  28.   scope :created, lambda { recent }
  29.   scope :popular, order('popularity DESC')
  30.   scope :recent, order('created_at DESC')
  31.   scope :free, where(for_free: true)
  32.  
  33.   monetize :standart_price_cents
  34.   monetize :extended_price_cents
  35.  
  36.   friendly_id :name, use: :slugged
  37.  
  38.   before_validation :extract_zip, :if => Proc.new { |model| model.zip }
  39.   before_create :store_orientation
  40.   after_save :push_jobs
  41.  
  42.   define_index do
  43.     indexes :name
  44.     indexes :description
  45.     indexes tags(:name), :as => :tag
  46.     indexes [primary_category.name, secondary_category.name], :as => :category
  47.     has :popularity
  48.     has :created_at
  49.     set_property :field_weights => {
  50.         :name => 100,
  51.         :description => 90,
  52.         :category => 80,
  53.         :tag => 70
  54.     }
  55.  
  56.     set_property :star => true
  57.   end
  58.  
  59.   def to_jq_upload
  60.     {
  61.         'name' => read_attribute(:name),
  62.         'size' => image.size,
  63.         'url' => thumb.url,
  64.         'delete_url' => 'gg',
  65.         'delete_type' => 'DELETE'
  66.     }
  67.   end
  68.  
  69.   def normalize_friendly_id(string)
  70.     super.gsub("_", "-")
  71.   end
  72.  
  73.   def tag_tokens=(val)
  74.     self.tags = val.split(',').map {|name| name.gsub(/\s+/, '').downcase }.uniq.map{|name|Tag.find_or_create_by_name(name) }
  75.   end
  76.  
  77.   def tag_tokens
  78.     tags.map(&:name).join(', ')
  79.   end
  80.  
  81.   def preview
  82.     make_thumb(:w => 300, :h => 315)
  83.   end
  84.  
  85.   def thumb
  86.     make_thumb(:w => 160, :h => 168)
  87.   end
  88.  
  89.   def zoom
  90.     make_thumb(:w => 769, :h => 800, :watermark => __method__.to_sym)
  91.   end
  92.  
  93.   def main
  94.     make_thumb(:w => 380, :h => 400, :watermark => __method__.to_sym)
  95.   end
  96.  
  97.   def popularity
  98.     super || 0
  99.   end
  100.  
  101.   def humanized_name
  102.     name.gsub(/_/,' ')
  103.   end
  104.  
  105.   def similar_vectors
  106.     tv = Arel::Table.new(:tags_vectors)
  107.     v = Vector.arel_table
  108.     t = Tag.arel_table  
  109.  
  110.     Vector.where(v[:id].in(
  111.       tv.project(tv[:vector_id])
  112.         .where(tv[:tag_id].in(
  113.           tv.project(tv[:tag_id])
  114.             .where(tv[:vector_id].eq(id)))
  115.             .and( tv[:vector_id].not_eq(id)))
  116.         .having(tv[:tag_id].count.gt(9))
  117.         .group(tv[:vector_id])))
  118.   end
  119.  
  120.   def self.full_search(q, params = {})
  121.     search(q,
  122.       :order => params[:order] || :created_at,
  123.       :sort_mode => params[:sort_mode] || :desc
  124.     )
  125.   end
  126.  
  127.   private
  128.  
  129.   def make_thumb(attrs = {})
  130.     extent = "#{attrs[:w]}x#{attrs[:h]}"
  131.     color = "#ebeaea"
  132.     if is_landscape
  133.       job = image.process(:convert, "-resize #{attrs[:w]}x -background \"#{color}\" -gravity center -extent #{extent}")
  134.     else
  135.       job = image.process(:convert, "-resize x#{attrs[:h]} -background \"#{color}\" -gravity center -extent #{extent}")
  136.     end
  137.     attrs[:watermark] ? job.process(:watermark, attrs[:watermark]) : job
  138.   end
  139.  
  140.   def extract_zip
  141.     raise "Not An Uploaded File" unless zip.is_a? ActionDispatch::Http::UploadedFile
  142.     ::Zip::Archive.open(zip.tempfile.to_path.to_s) do |ar|
  143.       raise "Only two files supported" unless ar.num_files == 2
  144.       self.name = name_from_tempfile
  145.       2.times do |i|
  146.         entry_name = ar.get_name(i)
  147.         ar.fopen(entry_name) do |f|
  148.           string = f.read
  149.           string.class.send(:define_singleton_method, :original_filename) {f.name}
  150.           case entry_name
  151.             when /.*\.eps\Z/
  152.               self.vector = string
  153.             when /.*\.jpg\Z/i
  154.               self.image = string
  155.             else
  156.               raise "Wrong file in Acrhive. Only EPS and JPG should persist"
  157.           end
  158.         end
  159.       end
  160.     end
  161.   end
  162.  
  163.  
  164.  
  165.   def name_from_tempfile
  166.     File.basename(zip.original_filename, '.*')
  167.   end
  168.  
  169.   def store_orientation
  170.     self.is_landscape = self.image.landscape?
  171.   end
  172.  
  173.   def push_jobs
  174.     [:main, :thumb, :preview, :zoom].each do |variant|
  175.       Delayed::Job.enqueue Omg::VectorJob.new(self, variant)
  176.     end
  177.   end
  178.  
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement