fables.rb 922 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. if db_column == "user_id"
  25. hash[db_column] = UID.match(field.value)[1]
  26. elsif db_column == "keywords"
  27. hash[db_column] = field.value.split(/\s?(,|\|)\s?/)
  28. else
  29. hash[db_column] = field.value
  30. end
  31. end
  32. hash = hash.reject { |k,v| k == nil }
  33. hash
  34. end
  35. end