image_controller.rb 1.1 KB

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