scinote-web/app/models/marvin_js_asset.rb

53 lines
1.5 KiB
Ruby
Raw Normal View History

2019-04-29 01:11:41 +08:00
# frozen_string_literal: true
2019-04-27 04:59:38 +08:00
2019-04-29 01:11:41 +08:00
class MarvinJsAsset < ApplicationRecord
2019-05-02 22:12:25 +08:00
validates :name, presence: true
validates :description, presence: true
validates :object_id, presence: true
validates :object_type, presence: true
2019-04-27 04:59:38 +08:00
belongs_to :object, polymorphic: true,
optional: true,
inverse_of: :marvin_js_assets
belongs_to :team, inverse_of: :marvin_js_assets, optional: true
def self.url
ENV['MARVINJS_URL']
end
def self.enabled?
ENV['MARVINJS_URL'] != nil
end
2019-04-29 01:11:41 +08:00
def self.add_sketch(values, team)
2019-04-28 04:54:59 +08:00
if values[:object_type] == 'TinyMceAsset'
2019-05-02 22:12:25 +08:00
tiny_mce_img = TinyMceAsset.create(
2019-04-28 04:54:59 +08:00
object: nil,
team_id: team.id,
saved: false,
image: values[:image],
image_file_name: "#{name}.jpg"
)
values[:object_id] = tiny_mce_img.id
end
2019-05-02 22:12:25 +08:00
values[:name] = I18n.t('marvinjs.new_sketch') if values[:name].empty?
2019-04-29 01:11:41 +08:00
create(values.merge(team_id: team.id).except(:image))
2019-04-27 04:59:38 +08:00
end
2019-05-02 22:12:25 +08:00
def self.update_sketch(values, team)
sketch = team.marvin_js_assets.find(values[:id])
return false unless sketch
2019-05-03 21:24:28 +08:00
values[:name] = I18n.t('marvinjs.new_sketch') if values[:name].empty?
sketch.update(values.except(:image, :object_type, :id))
2019-04-28 22:16:31 +08:00
if values[:object_type] == 'TinyMceAsset'
image = TinyMceAsset.find(sketch.object_id)
2019-05-03 21:24:28 +08:00
image.update(image: values[:image], image_file_name: "#{name}.jpg")
return { url: image.url(:large), description: sketch.description }
2019-04-28 22:16:31 +08:00
end
2019-04-29 01:11:41 +08:00
sketch
2019-04-28 22:16:31 +08:00
end
2019-04-27 04:59:38 +08:00
end