image_controller.rb 1.2 KB

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