Skip to content

Instantly share code, notes, and snippets.

@keown
Created March 19, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keown/8939d90d9a31379e9409 to your computer and use it in GitHub Desktop.
Save keown/8939d90d9a31379e9409 to your computer and use it in GitHub Desktop.
eccolooooo
class Image < ActiveRecord::Base
has_secure_token :uuid, key_length: 48
belongs_to :event
belongs_to :user
has_attached_file :image,
:styles => { :medium => "400x400>", :thumb => "200x200>" },
:default_url => "/images/:style/missing.png"
validates_attachment_content_type :image,
:content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
def get_context_array(context_type, context_id)
case context_type
when 'cosplayer'
user = User.find(context_id)
if user
images = user.images.order('created_at DESC')
else
false
end
else
event = Event.find(context_id)
if event
images = event.images.order('created_at DESC')
else
false
end
end
images
end
def previous_image(context_type, context_id)
array = self.get_context_array(context_type, context_id)
index = array.index(self)
if index == 0
current_index = array.length - 1
else
current_index = index - 1
end
array[current_index]
end
def next_image(context_type, context_id)
array = self.get_context_array(context_type, context_id)
index = array.index(self)
last_index = array.length - 1
if index == last_index
current_index = 0
else
current_index = index + 1
end
array[current_index]
end
def reactify(context_type, context_id)
tmp = Jbuilder.encode do |json|
json.(self.decorate(context: {context_type: context_type, context_id: context_id}), :id, :url, :characters, :cosplayers, :user, :event, :previous_image_uuid, :next_image_uuid)
end
JSON.parse(tmp)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment