Module: Slugable::HasSlug

Defined in:
lib/slugable/has_slug.rb

Instance Method Summary collapse

Instance Method Details

#has_slug(options = {}) ⇒ Object

USAGE generate before save filter for filling slug from name attribute, and parameterize slug attribute you can change default columns by passing :from and :to attributes

it also generate method to_slug (depanding od :to param), which generate slug url for link_path

has_slug # generate to_slug has_slug :from => :title # generate to_slug has_slug :to => :seo_url # generate to_url has_slug :from => :name, :to => :slug # generate to_slug



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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/slugable/has_slug.rb', line 15

def has_slug(options={})
  defaults = {:from => :name, :to => :slug, :formatter => :parameterize, :cache_tree => true}
  options.reverse_merge!(defaults)
  from = options.delete(:from)
  to = options.delete(:to)
  formatter = options.delete(:formatter)
  cache_tree = options.delete(:cache_tree)
  before_save :"fill_slug_from_#{from}_to_#{to}", :"format_slug_from_#{from}_to_#{to}"
  after_save :"update_my_#{to}_cache"

  # generate this
  #
  # def fill_slug
  #   self.slug = name if slug.blank? || slug.parameterize.blank?
  # end
  code =<<-method
    def fill_slug_from_#{from}_to_#{to}
      self.#{to} = #{from} if #{to}.blank? || #{to}.parameterize.blank?
    end
  method
  class_eval(code)

  # generate this
  #
  # def format_slug
  #   self.slug = slug.parameterize
  # end
  code =<<-method
    def format_slug_from_#{from}_to_#{to}
      self.#{to} = #{to}.send(:#{formatter})
    end
  method
  class_eval(code)

  # generate this
  # def update_my_slug_cache
  #  @@all ||= {}
  #  @@all[id] = send(:slug)
  # end
  code =<<-method
    def update_my_#{to}_cache
      @@all ||= {}
      @@all[id] = send(:#{to})
    end
  method
  class_eval(code)

  # generate this
  #
  # def self.all_slugs
  #  slug_column = :slug
  #  @@all ||= self.all.map_to_hash{|slug_element| {slug_element.id => slug_element.send(slug_column)}}
  # end
  code =<<-method
    def self.all_#{to}s
      @@all ||= self.all.map_to_hash{|slug_element| {slug_element.id => slug_element.send(:#{to})}}
    end
  method
  class_eval(code)

  # generate this
  #
  # def self.clear_cached_slugs
  #  @@all = nil
  # end
  code =<<-method
    def self.clear_cached_#{to}s
      @@all = nil
    end
  method
  class_eval(code)

  # generate this
  #
  # def self.cached_slug(id)
  #  all_slugs[id]
  # end
  code =<<-method
    def self.cached_#{to}(id)
      all_#{to}s[id].to_s
    end
  method
  class_eval(code)

  # generate this
  #
  # def to_slug
  #  if respond_to?(:path_ids)
  #    slugs = if true
  #      path_ids.map{|id| self.class.cached_slug(id)}.compact.select{|i| i.size > 0 }
  #    else
  #      path.map{|record| record.send(:"slug")}.compact.select{|i| i.size > 0 }
  #    end
  #    slugs.empty? ? "" : slugs
  #  else
  #    send(:slug)
  #  end
  # end
  code =<<-method
    def to_#{to}
      if respond_to?(:path_ids)
        slugs = if #{cache_tree}
          path_ids.map{|id| self.class.cached_#{to}(id)}.compact.select{|i| i.size > 0 }
        else
          path.map{|record| record.send(:"#{to}")}.compact.select{|i| i.size > 0 }
        end
        slugs.empty? ? "" : slugs
      else
        send(:#{to})
      end
    end
  method
  class_eval(code)


  # generate this
  #
  # def to_slug_was
  #  if respond_to?(:ancestry_was)
  #    old_slugs = if true
  #      ancestry_was.to_s.split("/").map { |ancestor_id| self.class.cached_slug(ancestor_id.to_i) }
  #    else
  #      ancestry_was.to_s.split("/").map { |ancestor_id| self.class.find(ancestor_id).send(:slug) }
  #    end
  #    old_slugs << send(:slug_was)
  #  else
  #    send(:slug_was)
  #  end
  # end
  code =<<-method
    def to_#{to}_was
      if respond_to?(:ancestry_was)
        old_slugs = if #{cache_tree}
          ancestry_was.to_s.split("/").map { |ancestor_id| self.class.cached_#{to}(ancestor_id.to_i) }
        else
          ancestry_was.to_s.split("/").map { |ancestor_id| self.class.find(ancestor_id).send(:#{to}) }
        end
        old_slugs << send(:#{to}_was)
      else
        send(:#{to}_was)
      end
    end
  method
  class_eval(code)

  # generate this
  #
  # def to_slug_will
  #  if respond_to?(:ancestry)
  #    old_slugs = if true
  #      ancestry.to_s.split("/").map { |ancestor_id| self.class.cached_slug(ancestor_id.to_i) }
  #    else
  #      ancestry.to_s.split("/").map { |ancestor_id| self.class.find(ancestor_id).send(:slug) }
  #    end
  #    old_slugs << send(:slug)
  #  else
  #    send(:slug_was)
  #  end
  # end
  code =<<-method
      def to_#{to}_will
        if respond_to?(:ancestry)
          new_slugs = if #{cache_tree}
            ancestry.to_s.split("/").map { |ancestor_id| self.class.cached_#{to}(ancestor_id.to_i) }
          else
            ancestry.to_s.split("/").map { |ancestor_id| self.class.find(ancestor_id).send(:#{to}) }
          end
          new_slugs << send(:#{to}).send(:#{formatter})
        else
          send(:#{to}).send(:#{formatter})
        end
      end
  method
  class_eval(code)
end