item.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. 'Effect' => 'effect',
  8. 'Side Effect' => 'side_effect',
  9. 'RP Reply' => 'rp_reply',
  10. 'Category' => 'category',
  11. 'Reusable' => 'reusable'
  12. }
  13. hash = {}
  14. app.fields.each do |field|
  15. next if field.nil?
  16. db_column = key_mapping[field.name]
  17. hash[db_column] =
  18. hash[db_column] = case db_column
  19. when 'category'
  20. field.value.split(',')
  21. when 'reusable'
  22. field.value == 'True'
  23. else
  24. field.value
  25. end
  26. end
  27. hash['name'] = app.title
  28. hash['description'] = app.description
  29. hash['url'] = app.thumbnail.url if app.thumbnail
  30. hash['edit_url'] = app.footer.text
  31. hash = hash.reject { |k, _v| k.nil? }
  32. hash
  33. end
  34. end