Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. # Module providing picture storage for given model
  2. module Picturable
  3. # Methods that are used in both HasOne and HasMany concerns
  4. module Shared
  5. # Method that acts as a direct alias for a picture thumb method
  6. # @param args [Array] arguments for Dragonfly thumb
  7. # @return [String] url for given thumb
  8. def thumb(*args)
  9. picture.thumb(*args)
  10. end
  11. end
  12.  
  13. # Include if you want to have multiple pictures for model
  14. module HasMany
  15. include Picturable::Shared
  16. extend ActiveSupport::Concern
  17.  
  18. # Orders in which picturable pictures can be sorted when using
  19. # the ordered scope
  20. AVAILABLE_PICTURES_ORDERS = [
  21. 'created_at ASC',
  22. 'created_at DESC',
  23. 'image_name ASC',
  24. 'image_name DESC'
  25. ]
  26.  
  27. included do
  28. class << self
  29. attr_accessor :require_pictures
  30. attr_accessor :main_picture
  31.  
  32. # @return [Boolean] should we require pictures
  33. def require_pictures?
  34. require_pictures
  35. end
  36. end
  37.  
  38. self.require_pictures = true
  39. self.main_picture = :last
  40.  
  41. enum pictures_order: AVAILABLE_PICTURES_ORDERS
  42.  
  43. delegate :thumb, to: :picture
  44.  
  45. has_many :pictures,
  46. as: :picturable,
  47. dependent: :destroy
  48.  
  49. scope :with_pictures, -> { joins(:pictures).includes(:pictures).distinct }
  50.  
  51. default_scope lambda {
  52. self.require_pictures? ? with_pictures : where(false)
  53. }
  54.  
  55. accepts_nested_attributes_for :pictures,
  56. allow_destroy: true
  57.  
  58. validates :pictures_order,
  59. presence: true
  60.  
  61. validates :pictures,
  62. length: { minimum: 1 },
  63. if: -> { self.class.require_pictures? }
  64.  
  65. # @return [Picture] first picture that will acts as a cover
  66. # It simplifies interface - both has one and has many will have
  67. # a picture method to return a cover
  68. def picture
  69. pictures.public_send(self.class.main_picture)
  70. end
  71. end
  72. end
  73. end
  74.  
  75.  
  76. # Single picture that can be attached to any models
  77. class Picture < ApplicationRecord
  78. extend Dragonfly::Model
  79. dragonfly_accessor :image
  80.  
  81. delegate :url, to: :image
  82.  
  83. alias_method :to_s, :url
  84.  
  85. belongs_to :picturable,
  86. polymorphic: true,
  87. counter_cache: true,
  88. touch: true,
  89. optional: true
  90.  
  91. scope :ordered, lambda {
  92. return self unless (parent = new.picturable)
  93. order(parent.pictures_order)
  94. }
  95.  
  96. dragonfly_accessor :image do
  97. after_assign do |attachment|
  98. # Auto orient all the images - so they will look as they should
  99. attachment.convert! '-auto-orient'
  100. self.aspect_ratio = attachment.aspect_ratio
  101. end
  102. end
  103.  
  104. # Max size of an image that can be uploaded
  105. MAX_SIZE = 15.megabytes
  106. # Available images formats (based on ImageMagick)
  107. AVAILABLE_FORMATS = %w( jpeg png gif )
  108.  
  109. validates :image,
  110. presence: true
  111. validates_size_of :image,
  112. maximum: MAX_SIZE
  113. validates_property :format,
  114. of: :image, in: AVAILABLE_FORMATS,
  115. message: I18n.t('activerecord.errors.models.picture.attributes.image.images_only')
  116.  
  117. # Method that acts as a direct alias for an image thumb but with additional
  118. # formatting handling. There's an issue with gif images - if we want to
  119. # make jpg thumb out of them, we have to flatten it and convert to png/jpg
  120. # that's why we check a params format here - if it is not gif, it will flatten
  121. # a given image
  122. # @param args [Array] arguments for Dragonfly thumb
  123. # @return [String] url for given thumb
  124. def thumb(*args)
  125. params = args.last.is_a?(Hash) ? args.last : {}
  126.  
  127. encode = params[:format] && params[:format] != 'gif'
  128.  
  129. if encode
  130. result = image.encode(params[:format], '-flatten').thumb(*args)
  131. else
  132. result = image.thumb(*args)
  133. end
  134.  
  135. # Sometimes this is a Dragonly processor and sometimes it is a direct
  136. # file link - so we need to check
  137. result.is_a?(String) ? result : result.url
  138. end
  139. end
  140.  
  141. # This model represents a single article/news/text etc
  142. class Text < ApplicationRecord
  143. include Picturable::HasMany
  144.  
  145. validates :title,
  146. presence: true
  147. validates :abstract,
  148. presence: true
  149. validates :content,
  150. presence: true
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement