Module: Polygallery::ActsAsPolyphoto::LocalInstanceMethods

Defined in:
lib/polygallery/acts_as_polyphoto.rb

Instance Method Summary collapse

Instance Method Details

#include_polygallery_settings(settings) ⇒ Object



66
67
68
69
# File 'lib/polygallery/acts_as_polyphoto.rb', line 66

def include_polygallery_settings(settings)
  self.polygallery_options = settings
  self.initialize_polyphoto
end

#init_attachmentObject



140
141
142
# File 'lib/polygallery/acts_as_polyphoto.rb', line 140

def init_attachment
  self.class.init_attachment paperclip_settings
end

#initialize_polyphotoObject



71
72
73
74
75
76
# File 'lib/polygallery/acts_as_polyphoto.rb', line 71

def initialize_polyphoto
  return if self.polyphoto_initialized.present?
  self.class.init_associations(self.polygallery_settings)
  self.init_attachment
  self.polyphoto_initialized = true
end

#paperclip_settingsObject



110
111
112
# File 'lib/polygallery/acts_as_polyphoto.rb', line 110

def paperclip_settings
  self.polygallery_settings[:paperclip]
end

#photo_remote_url=(url_value) ⇒ Object



158
159
160
161
# File 'lib/polygallery/acts_as_polyphoto.rb', line 158

def photo_remote_url=(url_value)
  self.photo = URI.parse process_remote_uri(url_value)
  @photo_remote_url = url_value
end

#polygallery_settingsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/polygallery/acts_as_polyphoto.rb', line 78

def polygallery_settings
  return self.polygallery_options if self.polygallery_options.present?
  if self.gallery_title.present?
    s = if self.galleryable.present?
          galleryable.class.send(:"#{self.gallery_title}_settings")
        elsif self.galleryable_type.present?
          Kernel.const_get(self.galleryable_type)
            .send(:"#{self.gallery_title}_settings")
        end
    return s if s.present?
  elsif respond_to?(:gallery) && gallery.present?
    return gallery.polygallery_settings
  elsif self.gallery_id.present?
    g = Polygallery::Gallery.find(self.gallery_id)
    if g.present?
      self.gallery_title = g.title
      return self.polygallery_settings
    end
  end
  self.class.polygallery_settings
  # if gallery_title.present? && galleryable_type.present?
  #   galleryable_class = Kernel.const_get(galleryable_type)
  #   galleryable_class.send :"#{gallery_title}_settings"
  # elsif gallery_title.present?
  #   self.send(gallery_title.to_sym).polygallery_settings
  # elsif self.gallery.present? && gallery.polygallery_settings.present?
  #   self.gallery.polygallery_settings 
  # else
  #   DEFAULT_SETTINGS
  # end
end

#process_photo_to_uploadObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/polygallery/acts_as_polyphoto.rb', line 114

def process_photo_to_upload
  # As crazy as this looks, it's necessary to assign paperclip's
  # dynamic styles through an association.  It would be great if we
  # could find a better way, though...
  #
  # This part runs the second try.
  if (new_record? || photo_updated_at_changed?) && gallery.present? &&
      photo_to_upload.present? && File.exists?(photo_to_upload)
    # puts 'Running the swaparoo!'
    File.open(photo_to_upload, 'rb') {|h| self.photo = h }
    # self.photo_to_upload = nil
  end
  # This part runs the first try.
  if self.photo.present? && self.photo_to_upload.nil?
    # if self.new_record? || self.photo_file_name_changed?
    if self.photo.staged?
      # puts "#{photo_file_name} is staged!"
      if File.exists? self.photo.staged_path
        # puts "Setting photo to upload to #{photo_file_name}"
        self.photo_to_upload = self.photo.staged_path
        # self.photo = nil
      end
    end
  end
end

#process_remote_uri(uri) ⇒ Object



153
154
155
156
157
# File 'lib/polygallery/acts_as_polyphoto.rb', line 153

def process_remote_uri(uri)
  require 'open-uri'
  require 'open_uri_redirections'
  open(uri, allow_redirections: :safe) {|r| r.base_uri.to_s }
end

#remote_url(style = nil) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/polygallery/acts_as_polyphoto.rb', line 144

def remote_url(style=nil)
  host = ActionController::Base.asset_host
  host = "http://#{host}" unless host.nil? || host =~ /^http/
  path = photo.url(style)
  url = (host||'') + (path||'')
  url unless url.blank?
end

#thumb_urlObject



151
# File 'lib/polygallery/acts_as_polyphoto.rb', line 151

def thumb_url; remote_url(:thumb) end