ruling_template/app/models/r_template.rb

43 lines
1.6 KiB
Ruby

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
field :git_url
field :git_branch
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
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
end