Module: SluggableFinder::Orm::InstanceMethods

Defined in:
lib/sluggable_finder/orm.rb

Instance Method Summary collapse

Instance Method Details

#create_sluggable_slugObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sluggable_finder/orm.rb', line 79

def create_sluggable_slug
  suffix = ''
  begin
  proposed_slug = if self.send(destination_column.to_sym).blank? # self.slug
    SluggableFinder.encode get_value_or_generate_random(source_column.to_sym) # self.title
  else
    SluggableFinder.encode get_value_or_generate_random(destination_column.to_sym) # self.slug
  end
  rescue Exception => e
  	raise e
  end
  cond = if new_record?
    ''
  else
    "id != #{id} AND "
  end
  slugable_class.transaction do
    #case insensitive
    existing = slugable_class.find(:first, :conditions => ["#{cond}#{destination_column} LIKE ? and #{scope_condition}",  proposed_slug + suffix])
    while existing != nil or sluggable_finder_options[:reserved_slugs].include?(proposed_slug + suffix)
      if suffix.empty?
        suffix = "-2"
      else
        suffix.succ!
      end
      existing = slugable_class.find(:first, :conditions => ["#{cond}#{destination_column} = ? and #{scope_condition}",  proposed_slug + suffix])
    end
  end # end of transaction         
  proposed_slug + suffix
end

#get_value_or_generate_random(column_name) ⇒ Object



74
75
76
77
# File 'lib/sluggable_finder/orm.rb', line 74

def get_value_or_generate_random(column_name)
  v = self.send(column_name)
  v || SluggableFinder.random_slug_for(self.class)
end

#set_slugObject



69
70
71
72
# File 'lib/sluggable_finder/orm.rb', line 69

def set_slug
  s = self.create_sluggable_slug
  write_attribute(destination_column, s)
end