image_controller.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 if image
  6. return
  7. end
  8. if image
  9. image.update(url: url, category: rating)
  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 = nsfw ? CharImage.where(char_id: char_id) :
  34. CharImage.where(char_id: char_id, category: 'SFW' )
  35. cur_i = img ? imgs.index { |i| i[:id] == img } : imgs.length - 1
  36. case dir
  37. when :left
  38. nex_i = cur_i == 0 ? imgs.length - 1 : cur_i - 1
  39. else
  40. nex_i = cur_i == imgs.length - 1 ? 0 : cur_i + 1
  41. end
  42. imgs[nex_i]
  43. end
  44. end