| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- class ImageController
- def self.default_image(url, char_id, rating)
- image = CharImage.where(char_id: char_id).find_by(keyword: 'Default')
- if url.nil?
- image&.destroy
- return
- end
- if image
- image.update(url: url)
- else
- CharImage.create(
- char_id: char_id,
- url: url,
- category: rating,
- keyword: 'Default'
- )
- end
- end
- def self.edit_image(params)
- img_hash = CharImage.from_form(params)
- char_id = img_hash['char_id']
- keyword = img_hash['keyword']
- img = CharImage.where(char_id: char_id).find_by(keyword: keyword)
- if img
- img.update(img_hash)
- img.reload
- else
- img = CharImage.create(img_hash)
- end
- img
- end
- def self.img_scroll(char_id:, nsfw: false, img: nil, dir: nil)
- imgs = if nsfw
- CharImage.where(char_id: char_id)
- else
- CharImage.where(char_id: char_id, category: 'SFW')
- end
- cur_i = img ? imgs.index { |i| i[:id] == img } : imgs.length - 1
- nex_i = case dir
- when :left
- cur_i == 0 ? imgs.length - 1 : cur_i - 1
- else
- cur_i == imgs.length - 1 ? 0 : cur_i + 1
- end
- imgs[nex_i]
- end
- end
|