module Permalink::ActiveRecord::InstanceMethods
Public Instance Methods
to_param()
click to toggle source
# File lib/permalink/active_record.rb, line 47 def to_param to_param_option = permalink_options[:to_param] to_param_option.compact.map do |name| respond_to?(name) ? public_send(name).to_s : name.to_s end.reject(&:blank?).join(permalink_options[:separator]) end
Private Instance Methods
build_scope_for_permalink()
click to toggle source
# File lib/permalink/active_record.rb, line 73 def build_scope_for_permalink search_scope = permalink_options[:scope] scope = self.class.unscoped if search_scope scope = scope.where(search_scope => public_send(search_scope)) end scope end
create_permalink()
click to toggle source
# File lib/permalink/active_record.rb, line 105 def create_permalink return unless update_permalink? permalink = Permalink.call( from_permalink_value.to_s, **permalink_generator_options ) write_attribute( to_permalink_name, next_available_permalink(permalink) ) end
from_permalink_name()
click to toggle source
# File lib/permalink/active_record.rb, line 84 def from_permalink_name permalink_options[:from_column_name] end
from_permalink_value()
click to toggle source
# File lib/permalink/active_record.rb, line 92 def from_permalink_value read_attribute(from_permalink_name) end
next_available_permalink(permalink)
click to toggle source
# File lib/permalink/active_record.rb, line 55 def next_available_permalink(permalink) unique_permalink = permalink scope = build_scope_for_permalink if permalink_options[:unique] suffix = 2 while scope.where(to_permalink_name => unique_permalink).first unique_permalink = [permalink, suffix].join(permalink_options[:separator]) suffix += 1 end end unique_permalink end
permalink_generator_options()
click to toggle source
# File lib/permalink/active_record.rb, line 119 def permalink_generator_options { separator: permalink_options[:separator], normalizations: permalink_options[:normalizations] } end
to_permalink_name()
click to toggle source
# File lib/permalink/active_record.rb, line 88 def to_permalink_name permalink_options[:to_column_name] end
to_permalink_value()
click to toggle source
# File lib/permalink/active_record.rb, line 96 def to_permalink_value read_attribute(to_permalink_name) end
update_permalink?()
click to toggle source
# File lib/permalink/active_record.rb, line 100 def update_permalink? changes[from_permalink_name] && (permalink_options[:force] || to_permalink_value.blank?) end