fables.rb 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. class Fable < ActiveRecord::Base
  2. validates :title, presence: true
  3. validates :story, presence: true
  4. def self.from_form(app)
  5. key_mapping = {
  6. 'Author' => 'user_id',
  7. 'Keywords' => 'keywords'
  8. }
  9. hash = {
  10. 'title' => nil,
  11. 'story' => nil,
  12. 'url' => nil,
  13. 'keywords' => nil,
  14. 'user_id' => nil,
  15. 'edit_url' => nil
  16. }
  17. hash['title'] = app.title
  18. hash['story'] = app.description
  19. hash['edit_url'] = app.footer.text
  20. hash['url'] = app.image&.url
  21. app.fields.each do |field|
  22. next if field.nil?
  23. db_column = key_mapping[field.name]
  24. hash[db_column] = if db_column == 'user_id'
  25. UID.match(field.value)[1]
  26. elsif db_column == 'keywords'
  27. field.value.split(/\s?(,|\|)\s?/)
  28. else
  29. field.value
  30. end
  31. end
  32. hash = hash.reject { |k, _v| k.nil? }
  33. hash
  34. end
  35. end