ruling_template/app/models/r_template.rb

64 lines
2.3 KiB
Ruby
Raw Normal View History

2017-08-02 09:07:55 +00:00
class RTemplate
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
field :title, as: :slug_title, :localize => true
field :description, :localize => true
field :price, :type => Integer
field :allowed, :type => Boolean, :default => true
field :user_id
field :colors, :type => Array, :default => []
field :sold, :type => Boolean, :default => false
2021-04-11 03:12:02 +00:00
field :git_url
field :git_branch
2021-05-11 02:25:04 +00:00
field :allow_domain #change from String to Array
2021-04-11 15:09:25 +00:00
field :installed_site_tokens, type: Array, default: []
2017-08-02 09:07:55 +00:00
mount_uploader :template_zip, AssetUploader
has_many :template_image_files, :autosave => true, :dependent => :destroy
has_one :template_psd_file, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :template_image_files, :allow_destroy => true
accepts_nested_attributes_for :template_psd_file, :allow_destroy => true
2021-05-11 02:25:04 +00:00
before_save do
if self.allow_domain.class == String
self.allow_domain = self.allow_domain.split(",").map{|s| s.strip}
elsif self.allow_domain.nil?
self.allow_domain = []
end
end
after_initialize do
unless self.new_record?
save_flag = false
if self.allow_domain.class == String
self.allow_domain = self.allow_domain.split(",").map{|s| s.strip}
save_flag = true
elsif self.allow_domain.nil?
self.allow_domain = []
save_flag = true
end
self.save(:validate=>false) if save_flag
end
end
2021-04-11 03:12:02 +00:00
def template_zip(origin_flag=false)
if self.git_url.blank? || self.git_branch.blank? || origin_flag
super()
else
uri = URI(self.git_url)
zip_end_url_map = {
'gitlab.com' => '-/archive/{branch}/gitlab-foss-{branch}.zip',
'github.com' => '/archive/refs/heads/{branch}.zip',
'gitlab.tp.rulingcom.com' => '/repository/archive.zip?ref={branch}',
'default' => '/repository/archive.zip?ref={branch}'
}
end_url = zip_end_url_map.keys.include?(uri.host) ? zip_end_url_map[uri.host] : zip_end_url_map['default']
path = self.git_url.gsub(/(\.git)*[\/]*$/, '') + end_url.gsub('{branch}',self.git_branch)
JSON.parse({'url' => path}.to_json, object_class: OpenStruct)
end
end
2017-08-02 09:07:55 +00:00
end