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 field :allow_domain #change from String to Array field :installed_site_tokens, type: Array, default: [] 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 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 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