osem-fcy/app/models/commercial.rb
Nishanth Vijayan a45e537bac Setup basic Revision History page
Sets up paper_trail tracking in important models

and add conference_id to versions for storing conference_id
of conference related objects  as metadata.This will help in querying
for versions related to conferences.

Fix issues with factories & event types initialization.Import paper_trail testing helpers
Fix bug: Creating an event creates a useless version with event update

Add revert and view changes features to revision history
2016-08-12 13:35:56 +05:30

54 lines
1.4 KiB
Ruby

class Commercial < ActiveRecord::Base
require 'oembed'
belongs_to :commercialable, polymorphic: true
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
validates :url, presence: true
validates :url, format: URI::regexp(%w(http https))
validate :valid_url
def self.render_from_url(url)
register_provider
begin
resource = OEmbed::Providers.get(url, maxwidth: 560, maxheight: 315)
{ html: resource.html.html_safe }
rescue StandardError => exception
{ error: exception.message }
end
end
private
def valid_url
result = Commercial.render_from_url(url)
if result[:error]
errors.add(:base, result[:error])
end
end
def self.register_provider
speakerdeck = OEmbed::Provider.new('http://speakerdeck.com/oembed.json')
speakerdeck << 'https://speakerdeck.com/*'
speakerdeck << 'http://speakerdeck.com/*'
OEmbed::Providers.register(
OEmbed::Providers::Youtube,
OEmbed::Providers::Vimeo,
OEmbed::Providers::Slideshare,
OEmbed::Providers::Flickr,
OEmbed::Providers::Instagram,
speakerdeck
)
end
def conference_id
case commercialable_type
when 'Conference' then commercialable_id
when 'Event' then Event.find(commercialable_id).program.conference_id
when 'Venue' then Venue.find(commercialable_id).conference_id
end
end
end