image_controller.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. class ImageController
  2. def self.default_image(url, char_id, rating)
  3. image = CharImage.where(char_id: char_id).find_by(keyword: 'Default')
  4. if url.nil?
  5. image&.destroy
  6. return
  7. end
  8. if image
  9. image.update(url: url)
  10. else
  11. CharImage.create(
  12. char_id: char_id,
  13. url: url,
  14. category: rating,
  15. keyword: 'Default'
  16. )
  17. end
  18. end
  19. def self.edit_image(params)
  20. img_hash = CharImage.from_form(params)
  21. char_id = img_hash['char_id']
  22. keyword = img_hash['keyword']
  23. img = CharImage.where(char_id: char_id).find_by(keyword: keyword)
  24. if img
  25. img.update(img_hash)
  26. img.reload
  27. else
  28. img = CharImage.create(img_hash)
  29. end
  30. img
  31. end
  32. def self.img_scroll(char_id:, nsfw: false, img: nil, dir: nil)
  33. imgs = if nsfw
  34. CharImage.where(char_id: char_id)
  35. else
  36. CharImage.where(char_id: char_id, category: 'SFW')
  37. end
  38. cur_i = img ? imgs.index { |i| i[:id] == img } : imgs.length - 1
  39. nex_i = case dir
  40. when :left
  41. cur_i == 0 ? imgs.length - 1 : cur_i - 1
  42. else
  43. cur_i == imgs.length - 1 ? 0 : cur_i + 1
  44. end
  45. imgs[nex_i]
  46. end
  47. end