item.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. class Item < ActiveRecord::Base
  2. validates :name, presence: true
  3. def self.from_form(app)
  4. key_mapping = {
  5. "Item Name" => "name",
  6. "Description" => "description",
  7. "Known Effects" => "effect",
  8. "Potential Side Effects" => "side_effect",
  9. "Limitations" => "limits",
  10. "Duration" => "duration",
  11. "RP Used Message" => "rp_use",
  12. "RP Find Message" => "rp_find",
  13. "Rating" => "rating",
  14. "Image URL" => "img_url",
  15. "Category" => "category",
  16. "Rarity" => "rarity",
  17. "Reusable" => "reusable",
  18. "Location" => "location",
  19. "Crafting Recipe" => "recipe",
  20. "Status List" => "statuses"
  21. }
  22. hash = {}
  23. app.fields.each do |field|
  24. next if field.nil?
  25. db_column = key_mapping[field.name]
  26. hash[db_column] =
  27. case db_column
  28. when "category"
  29. hash[db_column] = field.value.split(",")
  30. when "reusable"
  31. hash[db_column] = field.value == "True" ? true : false
  32. else
  33. hash[db_column] = field.value
  34. end
  35. end
  36. hash["name"] = app.title
  37. hash["description"] = app.description
  38. hash["url"] = app.thumbnail.url if app.thumbnail
  39. hash["edit_url"] = app.footer.text
  40. hash = hash.reject { |k,v| k == nil }
  41. hash
  42. end
  43. end