Class: MergeOldRevisionsHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/maintenance-helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(wiki, event_to_check_on = :day, minimum_age_to_merge = 60*60*24*365, maximum_time_between_revisions_for_merge = 60*60) ⇒ MergeOldRevisionsHelper

Default wakes up each hour and merges all revisions more than 24 hours old, by the same author, and that are created within an hour of each other



81
82
83
84
85
86
# File 'lib/helpers/maintenance-helpers.rb', line 81

def initialize( wiki, event_to_check_on = :day, minimum_age_to_merge = 60*60*24*365, maximum_time_between_revisions_for_merge = 60*60 ) 
	@wiki = wiki
	@minimum_age_to_merge = minimum_age_to_merge
	@maximum_time_between_revisions_for_merge = maximum_time_between_revisions_for_merge
	@wiki.watch_for(event_to_check_on) { check_for_pages_to_merge }
end

Instance Method Details

#can_merge?(revision_a, revision_b) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
# File 'lib/helpers/maintenance-helpers.rb', line 129

def can_merge?( revision_a, revision_b )
	return false unless revision_a && revision_b
	return false unless same_author?( revision_a, revision_b )
	return false unless revised_at_a_similar_time?( revision_a, revision_b )
	return false unless not_to_recent?( revision_a )
	return false unless not_to_recent?( revision_b )
	true
end

#check_for_pages_to_mergeObject



88
89
90
91
92
93
94
# File 'lib/helpers/maintenance-helpers.rb', line 88

def check_for_pages_to_merge
	@wiki.each do |name, page|
		page.content_lock.synchronize do
			check_revisions_to_merge_on page
		end
	end
end

#check_revisions_to_merge_on(page) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/helpers/maintenance-helpers.rb', line 96

def check_revisions_to_merge_on( page )
	return if page.empty?
	change_made = false
	new_revisions = []
	next_revision = page.revisions.first
	while next_revision
		ending_revision = next_revision
		
		while can_merge?( next_revision, ending_revision.following_revision )
			ending_revision = ending_revision.following_revision
		end
		 
		changes = 	if ending_revision == next_revision
						next_revision.changes
					else
						change_made = true
						ending_revision.content.changes_from( next_revision.previous_content )
					end
					
		new_revisions << Revision.new( 	page, 
										new_revisions.length, 
										changes, 
										ending_revision.author, 
										ending_revision.created_on )
										
		next_revision = ending_revision.following_revision
	end	
	if change_made
		page.revisions = new_revisions
		@wiki.save_all_revisions( page )
	end
end

#not_to_recent?(a_revision) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/helpers/maintenance-helpers.rb', line 146

def not_to_recent?( a_revision )
	(Time.now - a_revision.revised_on) > @minimum_age_to_merge
end

#revised_at_a_similar_time?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/helpers/maintenance-helpers.rb', line 142

def revised_at_a_similar_time?( a, b )
	(a.revised_on - b.revised_on).abs < @maximum_time_between_revisions_for_merge
end

#same_author?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/helpers/maintenance-helpers.rb', line 138

def same_author?( a, b )
	a.author == b.author
end