| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- class Fable < ActiveRecord::Base
- validates :title, presence: true
- validates :story, presence: true
- def self.from_form(app)
- key_mapping = {
- 'Author' => 'user_id',
- 'Keywords' => 'keywords'
- }
- hash = {
- 'title' => nil,
- 'story' => nil,
- 'url' => nil,
- 'keywords' => nil,
- 'user_id' => nil,
- 'edit_url' => nil
- }
- hash['title'] = app.title
- hash['story'] = app.description
- hash['edit_url'] = app.footer.text
- hash['url'] = app.image&.url
- app.fields.each do |field|
- next if field.nil?
- db_column = key_mapping[field.name]
- hash[db_column] = if db_column == 'user_id'
- UID.match(field.value)[1]
- elsif db_column == 'keywords'
- field.value.split(/\s?(,|\|)\s?/)
- else
- field.value
- end
- end
- hash = hash.reject { |k, _v| k.nil? }
- hash
- end
- end
|