dibbler 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +73 -0
- data/app/models/dibbler/slug.rb +17 -0
- data/config/locales/cs.yml +18 -0
- data/config/locales/en.yml +18 -0
- data/config/routes.rb +13 -0
- data/db/migrate/20150721151036_create_slugs.rb +11 -0
- data/lib/dibbler.rb +199 -0
- data/lib/dibbler/engine.rb +17 -0
- data/lib/dibbler/helpers/url_helper.rb +26 -0
- data/lib/dibbler/middlewares/locale.rb +62 -0
- data/lib/dibbler/middlewares/slug.rb +49 -0
- data/lib/dibbler/models/hierarchical_slug_generator.rb +148 -0
- data/lib/dibbler/models/slug.rb +414 -0
- data/lib/dibbler/models/slug_generator.rb +111 -0
- data/lib/dibbler/railtie.rb +18 -0
- data/lib/dibbler/utils/enum.rb +133 -0
- metadata +75 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# *****************************************************************************
|
|
2
|
+
# * Copyright (c) 2019 Matěj Outlý
|
|
3
|
+
# *****************************************************************************
|
|
4
|
+
# *
|
|
5
|
+
# * Linear slug generator
|
|
6
|
+
# *
|
|
7
|
+
# * Author: Matěj Outlý
|
|
8
|
+
# * Date : 21. 7. 2015
|
|
9
|
+
# *
|
|
10
|
+
# *****************************************************************************
|
|
11
|
+
|
|
12
|
+
module Dibbler
|
|
13
|
+
module Models
|
|
14
|
+
module SlugGenerator
|
|
15
|
+
extend ActiveSupport::Concern
|
|
16
|
+
|
|
17
|
+
included do
|
|
18
|
+
|
|
19
|
+
after_save :generate_slugs
|
|
20
|
+
before_destroy :destroy_slugs, prepend: true
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
module ClassMethods
|
|
25
|
+
|
|
26
|
+
def generate_slugs(options = {})
|
|
27
|
+
self.all.each do |item|
|
|
28
|
+
item.generate_slugs(options)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def disable_slug_generator
|
|
35
|
+
@disable_slug_generator = true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def enable_slug_generator
|
|
39
|
+
@disable_slug_generator = false
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# *************************************************************
|
|
43
|
+
# Hooks
|
|
44
|
+
# *************************************************************
|
|
45
|
+
|
|
46
|
+
def generate_slugs(options = {})
|
|
47
|
+
return if @disable_slug_generator
|
|
48
|
+
ActiveRecord::Base.transaction do
|
|
49
|
+
|
|
50
|
+
# Generate slug in this model
|
|
51
|
+
unless Dibbler.slug_model.nil?
|
|
52
|
+
I18n.available_locales.each do |locale|
|
|
53
|
+
self._destroy_slug_was(Dibbler.slug_model, locale)
|
|
54
|
+
self._generate_slug(Dibbler.slug_model, locale)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def destroy_slugs(options = {})
|
|
62
|
+
|
|
63
|
+
# Destroy slug of this model
|
|
64
|
+
unless Dibbler.slug_model.nil?
|
|
65
|
+
I18n.available_locales.each do |locale|
|
|
66
|
+
self._destroy_slug(Dibbler.slug_model, locale)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def url_original
|
|
73
|
+
if @url_original.nil?
|
|
74
|
+
@url_original = self._url_original
|
|
75
|
+
end
|
|
76
|
+
@url_original
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def compose_slug_translation(locale)
|
|
80
|
+
self._compose_slug_translation(locale)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
protected
|
|
84
|
+
|
|
85
|
+
# *************************************************************
|
|
86
|
+
# Callbacks to be defined in application
|
|
87
|
+
# *************************************************************
|
|
88
|
+
|
|
89
|
+
def _url_original
|
|
90
|
+
raise "To be defined in application."
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def _compose_slug_translation(locale)
|
|
94
|
+
raise "To be defined in application."
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def _generate_slug(slug_model, locale)
|
|
98
|
+
raise "To be defined in application."
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def _destroy_slug(slug_model, locale)
|
|
102
|
+
raise "To be defined in application."
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def _destroy_slug_was(slug_model, locale)
|
|
106
|
+
raise "To be defined in application."
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# *****************************************************************************
|
|
2
|
+
# * Copyright (c) 2019 Matěj Outlý
|
|
3
|
+
# *****************************************************************************
|
|
4
|
+
# *
|
|
5
|
+
# * Railtie for view helpers integration
|
|
6
|
+
# *
|
|
7
|
+
# * Author: Matěj Outlý
|
|
8
|
+
# * Date : 22. 7. 2015
|
|
9
|
+
# *
|
|
10
|
+
# *****************************************************************************
|
|
11
|
+
|
|
12
|
+
module Dibbler
|
|
13
|
+
class Railtie < Rails::Railtie
|
|
14
|
+
initializer "dibbler.helpers" do
|
|
15
|
+
ActionView::Base.send :include, Helpers::UrlHelper
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# *****************************************************************************
|
|
2
|
+
# * Copyright (c) 2019 Matěj Outlý
|
|
3
|
+
# *****************************************************************************
|
|
4
|
+
# *
|
|
5
|
+
# * Enum definition
|
|
6
|
+
# *
|
|
7
|
+
# * Author: Matěj Outlý
|
|
8
|
+
# * Date : 7. 1. 2015
|
|
9
|
+
# *
|
|
10
|
+
# *****************************************************************************
|
|
11
|
+
|
|
12
|
+
require "active_record"
|
|
13
|
+
|
|
14
|
+
module Dibbler
|
|
15
|
+
module Utils
|
|
16
|
+
module Enum
|
|
17
|
+
extend ActiveSupport::Concern
|
|
18
|
+
|
|
19
|
+
module ClassMethods
|
|
20
|
+
|
|
21
|
+
# Add new enum column
|
|
22
|
+
def enum_column(new_column, spec, options = {})
|
|
23
|
+
|
|
24
|
+
# Prepare internal structure
|
|
25
|
+
if @enums.nil?
|
|
26
|
+
@enums = {}
|
|
27
|
+
end
|
|
28
|
+
@enums[new_column] = {}
|
|
29
|
+
|
|
30
|
+
# Fill out internal structure
|
|
31
|
+
spec.each do |item|
|
|
32
|
+
|
|
33
|
+
# Value check
|
|
34
|
+
if item.is_a?(OpenStruct)
|
|
35
|
+
item = item.to_h
|
|
36
|
+
elsif item.is_a? Hash
|
|
37
|
+
# OK
|
|
38
|
+
else
|
|
39
|
+
item = {value: item}
|
|
40
|
+
end
|
|
41
|
+
unless item[:value]
|
|
42
|
+
raise "Enum definition cannot be empty."
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Identify special type
|
|
46
|
+
item[:special_type] = :integer if item[:value].is_a?(Integer)
|
|
47
|
+
|
|
48
|
+
# Retype to string
|
|
49
|
+
item[:value] = item[:value].to_s
|
|
50
|
+
|
|
51
|
+
# Label
|
|
52
|
+
unless item[:label]
|
|
53
|
+
item[:label] = I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{new_column.to_s}_values.#{item[:special_type] ? item[:special_type].to_s + "_" : ""}#{item[:value]}")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Other attributes
|
|
57
|
+
if options[:attributes]
|
|
58
|
+
options[:attributes].each do |attribute|
|
|
59
|
+
singular_attribute_name = attribute.to_s.singularize
|
|
60
|
+
plural_attribute_name = attribute.to_s.pluralize
|
|
61
|
+
if item[singular_attribute_name.to_sym].nil?
|
|
62
|
+
item[singular_attribute_name.to_sym] = I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{new_column.to_s}_#{plural_attribute_name}.#{item[:special_type] ? item[:special_type].to_s + "_" : ""}#{item[:value]}")
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Store
|
|
68
|
+
@enums[new_column][item[:value].to_s] = OpenStruct.new(item)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Obj method
|
|
72
|
+
define_method((new_column.to_s + "_obj").to_sym) do
|
|
73
|
+
column = new_column
|
|
74
|
+
|
|
75
|
+
# Get map Value => Object
|
|
76
|
+
enums = self.class.enums[column]
|
|
77
|
+
|
|
78
|
+
# Get possible special type
|
|
79
|
+
special_type = enums.values.first.special_type if enums.first && enums.values.first.special_type
|
|
80
|
+
|
|
81
|
+
# Get value and modify it according to special type
|
|
82
|
+
value = self.send(column)
|
|
83
|
+
value = value.to_i if special_type == :integer
|
|
84
|
+
|
|
85
|
+
return self.class.enums[column][value.to_s]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# All method
|
|
89
|
+
define_singleton_method(("available_" + new_column.to_s.pluralize).to_sym) do
|
|
90
|
+
column = new_column
|
|
91
|
+
return @enums[column].values
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# All values method
|
|
95
|
+
define_singleton_method(("available_" + new_column.to_s + "_values").to_sym) do
|
|
96
|
+
column = new_column
|
|
97
|
+
return @enums[column].values.map { |o| o.value.to_sym }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Label method
|
|
101
|
+
define_singleton_method((new_column.to_s + "_label").to_sym) do |value|
|
|
102
|
+
column = new_column
|
|
103
|
+
return @enums[column].values.select { |o| o.value.to_s == value.to_s }.map { |o| o.label }.first
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Default value
|
|
107
|
+
if options[:default]
|
|
108
|
+
before_validation do
|
|
109
|
+
column = new_column
|
|
110
|
+
default = options[:default]
|
|
111
|
+
if self.send(column).nil?
|
|
112
|
+
self.send(column.to_s + "=", default.to_s)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Get all defined enums
|
|
120
|
+
def enums
|
|
121
|
+
@enums
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Check if given column is enum defined on this model
|
|
125
|
+
def has_enum?(column)
|
|
126
|
+
!@enums.nil? && !@enums[column].nil?
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dibbler
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Matěj Outlý
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-04-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rails
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '4.2'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '4.2'
|
|
27
|
+
description: Dibbler is an engine containing entire backend for localized human readable
|
|
28
|
+
URLs generation and integration.
|
|
29
|
+
email:
|
|
30
|
+
- matej.outly@gmail.com
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- LICENSE
|
|
36
|
+
- README.md
|
|
37
|
+
- app/models/dibbler/slug.rb
|
|
38
|
+
- config/locales/cs.yml
|
|
39
|
+
- config/locales/en.yml
|
|
40
|
+
- config/routes.rb
|
|
41
|
+
- db/migrate/20150721151036_create_slugs.rb
|
|
42
|
+
- lib/dibbler.rb
|
|
43
|
+
- lib/dibbler/engine.rb
|
|
44
|
+
- lib/dibbler/helpers/url_helper.rb
|
|
45
|
+
- lib/dibbler/middlewares/locale.rb
|
|
46
|
+
- lib/dibbler/middlewares/slug.rb
|
|
47
|
+
- lib/dibbler/models/hierarchical_slug_generator.rb
|
|
48
|
+
- lib/dibbler/models/slug.rb
|
|
49
|
+
- lib/dibbler/models/slug_generator.rb
|
|
50
|
+
- lib/dibbler/railtie.rb
|
|
51
|
+
- lib/dibbler/utils/enum.rb
|
|
52
|
+
homepage: https://github.com/matej-outly/dibbler
|
|
53
|
+
licenses:
|
|
54
|
+
- MIT
|
|
55
|
+
metadata: {}
|
|
56
|
+
post_install_message:
|
|
57
|
+
rdoc_options: []
|
|
58
|
+
require_paths:
|
|
59
|
+
- lib
|
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '0'
|
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
70
|
+
requirements: []
|
|
71
|
+
rubygems_version: 3.0.6
|
|
72
|
+
signing_key:
|
|
73
|
+
specification_version: 4
|
|
74
|
+
summary: Human readable and localized URLs
|
|
75
|
+
test_files: []
|