Class: SimpleSlug::Adapter
- Inherits:
-
Object
- Object
- SimpleSlug::Adapter
- Defined in:
- lib/simple_slug/adapter.rb
Instance Attribute Summary collapse
-
#current_locale ⇒ Object
Returns the value of attribute current_locale.
-
#locales ⇒ Object
readonly
Returns the value of attribute locales.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add_suffix(slug_value) ⇒ Object
- #column_name(locale = I18n.locale) ⇒ Object
- #column_names ⇒ Object
- #each_locale ⇒ Object
- #finder_method ⇒ Object
- #generate(record, force: false) ⇒ Object
- #get(record) ⇒ Object
- #get_prev(record) ⇒ Object
- #history_slug_exists?(record, slug_value) ⇒ Boolean
-
#initialize(model) ⇒ Adapter
constructor
A new instance of Adapter.
- #model_slug_exists?(record, slug_value) ⇒ Boolean
- #normalize(base) ⇒ Object
- #reset(record) ⇒ Object
- #resolve(record, slug_value) ⇒ Object
- #save_history(record) ⇒ Object
- #set(record, value) ⇒ Object
- #slug_base(record) ⇒ Object
- #slug_exists?(record, slug_value) ⇒ Boolean
- #valid_locale?(locale) ⇒ Boolean
- #with_locale(locale) ⇒ Object
Constructor Details
#initialize(model) ⇒ Adapter
Returns a new instance of Adapter.
6 7 8 9 10 |
# File 'lib/simple_slug/adapter.rb', line 6 def initialize(model) @model = model @options = model. @locales = Array(@options[:locales] || [nil]) end |
Instance Attribute Details
#current_locale ⇒ Object
Returns the value of attribute current_locale.
4 5 6 |
# File 'lib/simple_slug/adapter.rb', line 4 def current_locale @current_locale end |
#locales ⇒ Object (readonly)
Returns the value of attribute locales.
3 4 5 |
# File 'lib/simple_slug/adapter.rb', line 3 def locales @locales end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/simple_slug/adapter.rb', line 3 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/simple_slug/adapter.rb', line 3 def @options end |
Instance Method Details
#add_suffix(slug_value) ⇒ Object
80 81 82 |
# File 'lib/simple_slug/adapter.rb', line 80 def add_suffix(slug_value) "#{slug_value}--#{rand(99999)}" end |
#column_name(locale = I18n.locale) ⇒ Object
28 29 30 |
# File 'lib/simple_slug/adapter.rb', line 28 def column_name(locale=I18n.locale) [[:slug_column], (locale if valid_locale?(locale))].compact.join('_') end |
#column_names ⇒ Object
24 25 26 |
# File 'lib/simple_slug/adapter.rb', line 24 def column_names locales.map{|l| column_name(l) } end |
#each_locale ⇒ Object
44 45 46 47 48 |
# File 'lib/simple_slug/adapter.rb', line 44 def each_locale locales.each do |l| with_locale(l || I18n.default_locale) { yield } end end |
#finder_method ⇒ Object
12 13 14 |
# File 'lib/simple_slug/adapter.rb', line 12 def finder_method [:history] ? :find_by : :find_by! end |
#generate(record, force: false) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/simple_slug/adapter.rb', line 62 def generate(record, force: false) each_locale do next unless force || record.should_generate_new_slug? simple_slug = normalize(slug_base(record)) simple_slug = "__#{record.id || rand(9999)}" if simple_slug.blank? && [:fallback_on_blank] return if simple_slug == get(record).to_s.sub(SimpleSlug::RESOLVE_SUFFIX_REGEXP, '') set(record, resolve(record, simple_slug)) end end |
#get(record) ⇒ Object
32 33 34 |
# File 'lib/simple_slug/adapter.rb', line 32 def get(record) record.send(column_name) end |
#get_prev(record) ⇒ Object
36 37 38 |
# File 'lib/simple_slug/adapter.rb', line 36 def get_prev(record) record.send("#{column_name}_was") end |
#history_slug_exists?(record, slug_value) ⇒ Boolean
106 107 108 109 110 111 |
# File 'lib/simple_slug/adapter.rb', line 106 def history_slug_exists?(record, slug_value) return false unless [:history] base_scope = SimpleSlug::HistorySlug.where(sluggable_type: record.class.name, slug: slug_value) base_scope = base_scope.where('sluggable_id != ?', record.id) if record.persisted? base_scope.exists? end |
#model_slug_exists?(record, slug_value) ⇒ Boolean
100 101 102 103 104 |
# File 'lib/simple_slug/adapter.rb', line 100 def model_slug_exists?(record, slug_value) base_scope = record.class.unscoped.where(column_name => slug_value) base_scope = base_scope.where('id != ?', record.id) if record.persisted? base_scope.exists? end |
#normalize(base) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/simple_slug/adapter.rb', line 72 def normalize(base) parameterize_args = ActiveSupport::VERSION::MAJOR > 4 ? {separator: '-'} : '-' normalized = I18n.transliterate(base).parameterize(**parameterize_args).downcase normalized = "_#{normalized}" if normalized =~ SimpleSlug::STARTS_WITH_NUMBER_REGEXP normalized = normalized[0..[:max_length].pred] if [:max_length] normalized end |
#reset(record) ⇒ Object
50 51 52 |
# File 'lib/simple_slug/adapter.rb', line 50 def reset(record) each_locale{ set record, get_prev(record) } end |
#resolve(record, slug_value) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/simple_slug/adapter.rb', line 88 def resolve(record, slug_value) return slug_value unless slug_exists?(record, slug_value) loop do slug_with_suffix = add_suffix(slug_value) break slug_with_suffix unless slug_exists?(record, slug_with_suffix) end end |
#save_history(record) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/simple_slug/adapter.rb', line 54 def save_history(record) each_locale do slug_was = record.saved_change_to_attribute(column_name).try!(:first) next if slug_was.blank? ::SimpleSlug::HistorySlug.where(sluggable_type: record.class.name, slug: slug_was, locale: current_locale).first_or_initialize.update(sluggable_id: record.id) end end |
#set(record, value) ⇒ Object
40 41 42 |
# File 'lib/simple_slug/adapter.rb', line 40 def set(record, value) record.send("#{column_name}=", value) end |
#slug_base(record) ⇒ Object
84 85 86 |
# File 'lib/simple_slug/adapter.rb', line 84 def slug_base(record) [:slug_method].map{|m| record.send(m).to_s }.reject(&:blank?).join(' ') end |
#slug_exists?(record, slug_value) ⇒ Boolean
96 97 98 |
# File 'lib/simple_slug/adapter.rb', line 96 def slug_exists?(record, slug_value) model_slug_exists?(record, slug_value) || history_slug_exists?(record, slug_value) end |
#valid_locale?(locale) ⇒ Boolean
16 17 18 |
# File 'lib/simple_slug/adapter.rb', line 16 def valid_locale?(locale) locales.include?(locale) end |
#with_locale(locale) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/simple_slug/adapter.rb', line 113 def with_locale(locale) if defined? Globalize Globalize.with_locale(locale) do I18n.with_locale(locale) { yield } end else I18n.with_locale(locale) { yield } end end |