<commit_before>#$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require 'mongo_mapper'

module MongoMapper
	module Plugins
		module Draft
		  
		  # ClassMethods module will automatically get extended
		  module ClassMethods
		  end
		
		  # InstanceMethods module will automatically get included
		  module InstanceMethods

				def draft?
					self.draft
				end
				
				def published?
					if draft?
	    			if (self.class.find(self.draft_record_published_id) != nil)
	    				return true
  					end
    			else
    				return true
					end
					false
				end
		    
		    def publish
		    	if (draft? == false) # don't publish non-drafts... 
		    		return false
	    		end
	    		
		    	if (self.changed?) # save any changes, in case publish is called directly instead of save
		    		if (self.save == false)
		    			return false
		    		end
	    		end

					rec = nil
	    		# remove parents from previous published record
	    		if (self.respond_to?("parent_id_field") && self.respond_to?("path_field") && self.respond_to?("depth_field"))
		    		if (self.published?)
		    			#keep id
		    			rec = self.published_record
		    			rec.parent = nil
		    			rec.save
	    			end
    			end
	    		
	    		live_record = self.clone
	    		
	    		if (rec != nil)
	    			live_record._id = rec._id
    			end
    			
		    	self.draft_record_published_id = live_record._id
		    	self.save!

		  		# check if ramdiv-acts_as_tree is present
	    		if (self.respond_to?("parent_id_field") && self.respond_to?("path_field") && self.respond_to?("depth_field"))
		    		# if so, removee the current parent
		    		# set parent to nil for live_record before setting "real" parent.
		    		live_record.parent = nil
	    			
		    		if (self.parent != nil)
			    		# no need to copy order value, as it's copied in the clone process
			    		# check draft.parent.published_record != nil, and set as parent
			    		if (self.parent != nil)
			    			if (self.parent.published_record != nil)
			    				live_record.parent = self.parent.published_record
		    				end
		    			end
	    			end
    			end
    			
	      	live_record.draft = false;
		    	live_record.save!
		    	return true
	    	end
	    	
	    	def published_record
	    		if draft?
	    			self.class.find(self.draft_record_published_id)
    			else
    				self
  				end
    		end
    		
    		def draft_record
    			if draft?
    				self
  				else
  					self.class.all(:conditions => { :draft => true, :draft_record_published_id => self._id }).first
					end
  			end
    		
    		def unpublish
    			if draft?
	    			published = self.class.find(self.draft_record_published_id)
	    			if (published != nil)
	    				published.destroy
	  				end
					end
  			end
  			
		  end # InstanceMethods
		  
		  def self.configure(model)
		    #puts "Configuring #{model}..."
		    model.key :draft, Boolean, :default => true
		
			  model.key :draft_record_published_id, ObjectId # points to the live page version if draft == true. else NIL
			  
			  # Before destroy callback to remove published record
			  model.before_destroy :unpublish
		  end
		end
	end
end
<commit_msg>Extend ActiveSupport:Concert Minor Typos fixed<commit_after>#$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require 'mongo_mapper'

module MongoMapper
	module Plugins
		module Draft
 		  extend ActiveSupport::Concern
		  # ClassMethods module will automatically get extended
		  module ClassMethods
		  end
		
		  # InstanceMethods module will automatically get included
		  module InstanceMethods

				def draft?
					self.draft
				end
				
				def published?
					if draft?
	    			if (self.class.find(self.draft_record_published_id) != nil)
	    				return true
  					end
    			else
    				return true
					end
					false
				end
		    
		    def publish
		    	if (draft? == false) # don't publish non-drafts... 
		    		return false
	    		end
	    		
		    	if (self.changed?) # save any changes, in case publish is called directly instead of save
		    		if (self.save == false)
		    			return false
		    		end
	    		end

				rec = nil
	    		# remove parents from previous published record
	    		if (self.respond_to?("parent_id_field") && self.respond_to?("path_field") && self.respond_to?("depth_field"))
		    		if (self.published?)
		    			#keep id
		    			rec = self.published_record
		    			rec.parent = nil
		    			rec.save
	    			end
    			end
	    		
	    		live_record = self.clone
	    		
	    		if (rec != nil)
	    			live_record._id = rec._id
    			end
    			
		    	self.draft_record_published_id = live_record._id
		    	self.save!

		  		# check if ramdiv-acts_as_tree is present
	    		if (self.respond_to?("parent_id_field") && self.respond_to?("path_field") && self.respond_to?("depth_field"))
		    		# if so, remove the current parent
		    		# set parent to nil for live_record before setting "real" parent.
		    		live_record.parent = nil
	    			
		    		if (self.parent != nil)
			    		# no need to copy order value, as it's copied in the clone process
			    		# check draft.parent.published_record != nil, and set as parent
			    		if (self.parent != nil)
			    			if (self.parent.published_record != nil)
			    				live_record.parent = self.parent.published_record
		    				end
		    			end
	    			end
    			end
    			
	      	live_record.draft = false;
		    	live_record.save!
		    	return true
	    	end
	    	
	    	def published_record
	    		if draft?
	    			self.class.find(self.draft_record_published_id)
    			else
    				self
  				end
    		end
    		
    		def draft_record
    			if draft?
    				self
  				else
  					self.class.all(:conditions => { :draft => true, :draft_record_published_id => self._id }).first
					end
  			end
    		
    		def unpublish
    			if draft?
	    			published = self.class.find(self.draft_record_published_id)
	    			if (published != nil)
	    				published.destroy
	  				end
					end
  			end
  			
		  end # InstanceMethods
		  
		  def self.configure(model)
		    #puts "Configuring #{model}..."
		    model.key :draft, Boolean, :default => true
		
			  model.key :draft_record_published_id, ObjectId # points to the live page version if draft == true. else NIL
			  
			  # Before destroy callback to remove published record
			  model.before_destroy :unpublish
		  end
		end
	end
end

{'fraction_non_alphanumeric': 0.07699545240708797, 'fraction_numerical': 0.0, 'mean_word_length': 1.8652291105121295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1764583', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2038, 'n_words': 518}