image_controller.rb 1.1 KB

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