landmarks.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. class Landmark < ActiveRecord::Base
  2. validates :name, presence: true
  3. validates :description, presence: true
  4. validates :category, presence: true
  5. def self.from_form(app)
  6. key_mapping = {
  7. "Description" => "description",
  8. "Type" => "category",
  9. "Main Region" => "region",
  10. "Parent Landmark" => "location",
  11. "Rating" => "rating",
  12. "Kinks" => "kink",
  13. "Warning Description" => "warning",
  14. "Warning URL" => "w_url",
  15. "Warning Rating" => "w_rating",
  16. "History" => "history",
  17. "Folklore" => "folk_lore",
  18. "Layout URL" => "layout_url"
  19. }
  20. hash = {
  21. "name" => nil,
  22. "description" => nil,
  23. "category" => nil,
  24. "url" => nil,
  25. "location" => nil,
  26. "region" => nil,
  27. "warning" => nil,
  28. "w_url" => nil,
  29. "rating" => nil,
  30. "kink" => nil,
  31. "layout_url" => nil,
  32. "user_id" => nil,
  33. "w_rating" => nil,
  34. "history" => nil,
  35. "folk_lore" => nil,
  36. "edit_url" => nil
  37. }
  38. user_id = UID.match(app.description)
  39. hash["user_id"] = case app.description
  40. when /server/i
  41. 'Server'
  42. else
  43. user_id[1]
  44. end
  45. hash["name"] = app.title
  46. hash["edit_url"] = app.footer.text
  47. hash["url"] = app.image&.url
  48. app.fields.each do |field|
  49. next if field.nil?
  50. db_column = key_mapping[field.name]
  51. if db_column == "region"
  52. r = Region.find_by(name: field.value)
  53. hash[db_column] = r.id
  54. elsif db_column == "location"
  55. lm = Landmark.find_by(name: field.value)
  56. hash[db_column] = lm.id
  57. elsif db_column == "kink"
  58. hash[db_column] = field.value.split(/,\s?/)
  59. else
  60. hash[db_column] = field.value
  61. end
  62. end
  63. hash = hash.reject { |k,v| k == nil }
  64. hash
  65. end
  66. end