Module: Sequel::Plugins::Slugging::DatasetMethods

Defined in:
lib/sequel/plugins/slugging.rb

Instance Method Summary collapse

Instance Method Details

#from_slug(pk_or_slug) ⇒ Object



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
# File 'lib/sequel/plugins/slugging.rb', line 149

def from_slug(pk_or_slug)
  pk = model.primary_key

  case pk_type = model.pk_type
  when :integer
    case pk_or_slug
    when Integer
      where(pk => pk_or_slug).first
    when String
      if pk_or_slug =~ INTEGER_REGEX
        where(pk => pk_or_slug.to_i).first
      else
        lookup_by_slug(pk_or_slug)
      end
    else
      raise "Argument to Dataset#from_slug needs to be a String or Integer"
    end
  when :uuid
    if record = lookup_by_slug(pk_or_slug)
      record
    elsif pk_or_slug =~ UUID_REGEX
      where(pk => pk_or_slug).first
    end
  else
    raise "Unexpected pk_type: #{pk_type.inspect}"
  end
end

#from_slug!(pk_or_slug) ⇒ Object



145
146
147
# File 'lib/sequel/plugins/slugging.rb', line 145

def from_slug!(pk_or_slug)
  from_slug(pk_or_slug) || raise(Sequel::NoMatchingRow)
end

#lookup_by_slug(slug) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/sequel/plugins/slugging.rb', line 177

def lookup_by_slug(slug)
  if history = model.slugging_opts[:history]
    m = model
    if pk = m.db[history].where(sluggable_type: m.to_s, slug: slug).get(:sluggable_id)
      where(m.primary_key => pk).first
    end
  else
    where(slug: slug).first
  end
end