Skip to content

Commit

Permalink
Use read_attribute, write_attribute, attribute_changed? instead of se…
Browse files Browse the repository at this point in the history
…nd(:method_name)
  • Loading branch information
tiendung committed Apr 1, 2011
1 parent ac0f8db commit bf7b220
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/mongoid/slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ def slug(*fields, &block)
options = fields.extract_options!
self.slug_scope = options[:scope]
self.slug_name = options[:as] || :slug
self.slugged_fields = fields
self.slugged_fields = fields.map(&:to_s)

self.slug_builder =
if block_given?
block
else
lambda do |doc|
slugged_fields.map { |f| doc.send(f) }.join(',')
slugged_fields.map { |f| doc.read_attribute(f) }.join(',')
end
end

Expand Down Expand Up @@ -106,7 +106,7 @@ def slug!

# Returns the slug.
def to_param
self.send(slug_name)
read_attribute(slug_name)
end

private
Expand All @@ -121,7 +121,7 @@ def find_unique_slug
# Get the maximum counter slug
max_counter_slug = uniqueness_scope.only(slug_name).
where(slug_name => pattern, :_id.ne => _id).
order_by([slug_name, :desc]).first.try(:[], slug_name)
order_by([slug_name, :desc]).first.try(:read_attribute, slug_name)

if max_counter_slug
max_counter = max_counter_slug.match(/-(\d+)$/).try(:[], 1).to_i
Expand All @@ -139,11 +139,11 @@ def generate_slug
end

def generate_slug!
self.send("#{slug_name}=", find_unique_slug)
write_attribute(slug_name, find_unique_slug)
end

def slugged_fields_changed?
slugged_fields.any? { |f| self.send("#{f}_changed?") }
slugged_fields.any? { |f| attribute_changed?(f) }
end

def uniqueness_scope
Expand Down

0 comments on commit bf7b220

Please sign in to comment.