Module: FriendlyId::DataMapperAdapter::SluggedModel

Includes:
Slugged::Model
Defined in:
lib/friendly_id/datamapper_adapter/slugged_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/friendly_id/datamapper_adapter/slugged_model.rb', line 11

def self.included(base)
  base.class_eval do
    has n, :slugs,
      :model      => ::Slug,
      :child_key  => [:sluggable_id],
      :conditions => { :sluggable_type => base },
      :order      => [:id.desc]

    before :save do
      begin
        build_slug if new_slug_needed?
        method = friendly_id_config.method
      rescue FriendlyId::BlankError
        @errors ||= ValidationErrors.new
        @errors[method] = "can't be blank"
        throw :halt, false
      rescue FriendlyId::ReservedError
        @errors ||= ValidationErrors.new
        @errors[method] = "is reserved"
        throw :halt, false
      end
    end

    after(:save) do
      throw :halt, false if friendly_id_config.allow_nil? && !slug

      slug.sluggable_id = id
      slug.save
      set_slug_cache
    end

    after :update, :update_scope
    after :update, :update_dependent_scopes

    before(:destroy) do
      slugs.destroy!
    end
  end

  base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def self.get(*key)
      options = key.extract_options!

      if key.size == 1
        return super if key.first.is_a?(Array) || key.first.unfriendly_id?
        name, sequence = key.first.to_s.parse_friendly_id

        if !friendly_id_config.scope? && friendly_id_config.cache_column?
          result = self.first(friendly_id_config.cache_column => key.first)
        end

        conditions = {
          slugs.name     => name,
          slugs.sequence => sequence
        }
        conditions.merge!({
          slugs.scope    => (options[:scope].to_param if options[:scope] && options[:scope].respond_to?(:to_param))
        }) if friendly_id_config.scope?

        result ||= self.first(conditions)
        return super unless result
        result.friendly_id_status.name = name
        result.friendly_id_status.sequence = sequence
        result
      else
        super
      end
    end

    def self.get!(*key)
      return super unless friendly_id_config.scope?

      result = get(*key)
      if result
        result
      else
        options = key.extract_options!
        scope   = options[:scope]
        message = "Could not find \#{self.name} with key \#{key.inspect}"
        message << " and scope \#{scope.inspect}" if scope
        message << ". Scope expected but none given." unless scope
        raise(::DataMapper::ObjectNotFoundError, message)
      end
    end
  RUBY
end

Instance Method Details

#find_slug(name, sequence) ⇒ Object



102
103
104
# File 'lib/friendly_id/datamapper_adapter/slugged_model.rb', line 102

def find_slug(name, sequence)
  @slug = slugs.first(:name => name, :sequence => sequence)
end

#slugObject



98
99
100
# File 'lib/friendly_id/datamapper_adapter/slugged_model.rb', line 98

def slug
  @slug ||= slugs.first
end

#to_paramObject

Returns the friendly id, or if none is available, the numeric id. Note that this method will use the cached_slug value if present, unlike #friendly_id.



108
109
110
# File 'lib/friendly_id/datamapper_adapter/slugged_model.rb', line 108

def to_param
  friendly_id_config.cache_column ? to_param_from_cache : to_param_from_slug
end