landmarks.rb 1.8 KB

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