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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4bcfbc952b12ac5e2238a7d51f535072c611a7806e51c748da0e46c757fd5864
4
+ data.tar.gz: 4f6ac5c65ab5f5a887fb1900e024fa26b0f09d0039a8809f6bf9539353406455
5
+ SHA512:
6
+ metadata.gz: bcef9236447f561223e8daa5e2aad79eb8a211c43cd8380144453332b0463dd6a205351b3883df3994cefa660e80c67fe8b25b8d374cf3867343d8208a040535
7
+ data.tar.gz: 24e30c664a953bcb3678affa9481f4eca6b02550d0d4c5a11d222bbf7637a36f4c0d5c1f9fccec131650be525de01e749e75bd99a82e923b93794a9ed6c4b039
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Matěj Outlý
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Dibbler
2
+ Most enterprisingly unsuccessful entrepreneur. With this gem you can generate,
3
+ manage and integrate pretty (human readable and localized) URL addresses into
4
+ the web application. It uses DB table `slugs` on the background for storing URL
5
+ translations for all supported languages.
6
+
7
+ ## Installation
8
+
9
+ Add gem to your Gemfile:
10
+
11
+ ```ruby
12
+ gem "dibbler"
13
+ ```
14
+
15
+ Add database migrations to you application (you can modify DB structure
16
+ accordingly before migrating):
17
+
18
+ $ rake dibbler:install:migrations
19
+ $ rake db:migrate
20
+
21
+ ## Filters
22
+
23
+ Content of DB table `slugs` can be simply segmented into several subsets with
24
+ `filter` attribute. If filters are enabled, translation table loads only slugs
25
+ with filter attribute matching the the current filter defined by
26
+ `current_app_filter` option. With this system, you can store slugs for different
27
+ application variants (subdomains) in a single DB table (if many application
28
+ variants uses the same database).
29
+
30
+ ```ruby
31
+ Dibbler.setup do |config|
32
+ config.use_filter = true
33
+ config.current_app_filter = Rails.configuration.theme # contains "subdomain1", "subdomain2", etc.
34
+ config.available_filter_urls = {
35
+ subdomain1: "http://subdomain1.sample.com",
36
+ subdomain2: "http://subdomain2.sample.com",
37
+ subdomain3: "http://subdomain3.sample.com",
38
+ subdomain4: "http://subdomain4.sample.com",
39
+ subdomain5: "http://subdomain5.sample.com",
40
+ subdomain6: "http://subdomain6.sample.com",
41
+ subdomain7: "http://subdomain7.sample.com",
42
+ subdomain7: "http://subdomain8.sample.com",
43
+ }
44
+ ...
45
+ end
46
+ ```
47
+
48
+ ## Static slugs
49
+
50
+ You can define set of slugs which is integrated into translation tables by
51
+ default (not necessary to generate it in generators). Static slugs can be
52
+ configured in module configuration file:
53
+
54
+ ```ruby
55
+ Dibbler.setup do |config|
56
+ config.static_slugs = [
57
+ { locale: "cs", original: "search", translation: "vysledky-vyhledavani" },
58
+ { locale: "en", original: "search", translation: "search-results" },
59
+ { locale: "cs", original: "gallery", translation: "fotogalerie" },
60
+ { locale: "en", original: "gallery", translation: "photogallery" },
61
+ ]
62
+ ...
63
+ end
64
+ ```
65
+
66
+ Additionaly you can define `filter` attribute in each static slugs table.
67
+
68
+ ## Other configuration options
69
+
70
+ Other supported configuration options are:
71
+
72
+ - disable_default_locale
73
+ - downcase_translations
@@ -0,0 +1,17 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Slug
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 21. 1. 2016
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ module Dibbler
13
+ class Slug < ActiveRecord::Base
14
+ include Dibbler::Utils::Enum
15
+ include Dibbler::Models::Slug
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ cs:
2
+ activerecord:
3
+ attributes:
4
+ dibbler/slug:
5
+ locale: Jazyk
6
+ original: Systémové URL
7
+ translation: Hezké URL
8
+ filter: Syst. zn. aplikace
9
+ notices:
10
+ models:
11
+ dibbler/slug:
12
+ create: "Překlad URL byl úspěšně vytvořen."
13
+ update: "Překlad URL byl úspěšně upraven."
14
+ destroy: "Překlad URL byl úspěšně smazán."
15
+ errors:
16
+ models:
17
+ dibbler/slug:
18
+ not_found: "Překlad URL nebyl nalezen."
@@ -0,0 +1,18 @@
1
+ en:
2
+ activerecord:
3
+ attributes:
4
+ dibbler/slug:
5
+ locale: Language
6
+ original: System URL
7
+ translation: Pretty URL
8
+ filter: Application reference
9
+ notices:
10
+ models:
11
+ dibbler/slug:
12
+ create: URL translation was successfully saved.
13
+ update: URL translation was successfully created.
14
+ destroy: URL translation was successfully deleted.
15
+ errors:
16
+ models:
17
+ dibbler/slug:
18
+ not_found: URL translation was not found.
data/config/routes.rb ADDED
@@ -0,0 +1,13 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Routes
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 26. 11. 2015
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ Dibbler::Engine.routes.draw do
13
+ end
@@ -0,0 +1,11 @@
1
+ class CreateSlugs < ActiveRecord::Migration
2
+ def change
3
+ create_table :slugs do |t|
4
+ t.timestamps null: true
5
+ t.string :locale, index: true
6
+ # t.string :filter, index: true # Optional column distinguishing application variant
7
+ t.string :original, index: true
8
+ t.string :translation, index: true
9
+ end
10
+ end
11
+ end
data/lib/dibbler.rb ADDED
@@ -0,0 +1,199 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Dibbler engine
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 9. 6. 2015
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ # Engines
13
+ require "dibbler/engine"
14
+
15
+ # Middlewares
16
+ require "dibbler/middlewares/locale"
17
+ require "dibbler/middlewares/slug"
18
+
19
+ # Models
20
+ require "dibbler/models/slug"
21
+ require "dibbler/models/slug_generator"
22
+ require "dibbler/models/hierarchical_slug_generator"
23
+
24
+ # Helpers
25
+ require "dibbler/helpers/url_helper"
26
+
27
+ # Utils
28
+ require "dibbler/utils/enum"
29
+
30
+ module Dibbler
31
+
32
+ # This will keep Rails Engine from generating all table prefixes with the engines name
33
+ def self.table_name_prefix
34
+ end
35
+
36
+ # *************************************************************************
37
+ # Interface
38
+ # *************************************************************************
39
+
40
+ def self.disassemble(path)
41
+ # TODO work correctly with http:// and https:// links
42
+
43
+ # Match locale from path
44
+ match = /^\/(#{I18n.available_locales.join("|")})\//.match(path + "/")
45
+ if match
46
+ locale = match[1].to_sym
47
+ else
48
+ locale = nil
49
+ end
50
+
51
+ # Remove locale from path
52
+ if locale
53
+ path = path[(1 + locale.to_s.length)..-1]
54
+ end
55
+
56
+ # Root
57
+ if path.blank?
58
+ path = "/"
59
+ end
60
+
61
+ return [locale, path]
62
+ end
63
+
64
+ def self.assemble(locale, path)
65
+ if locale && (Dibbler.disable_default_locale || (I18n.default_locale.to_sym != locale.to_sym))
66
+ if path == "/"
67
+ path = "/" + locale.to_s
68
+ elsif path.starts_with?("http")
69
+ split1 = path.split("//")
70
+ split2 = split1[1].to_s.split("/")
71
+ split1[1] = split2.insert(1, locale.to_s).join("/")
72
+ path = split1.join("//")
73
+ else
74
+ path = "/" + locale.to_s + path
75
+ end
76
+ end
77
+ return path
78
+ end
79
+
80
+ def self.localify(path)
81
+ return path if path == "#"
82
+
83
+ # Match locale from path
84
+ locale, path = self.disassemble(path)
85
+
86
+ # Take current locale if path locale not defined
87
+ if locale.nil?
88
+ locale = I18n.locale
89
+ end
90
+
91
+ # Assemble back into path with locale
92
+ path = self.assemble(locale, path)
93
+
94
+ return path
95
+ end
96
+
97
+ def self.slugify(path)
98
+
99
+ # Match locale from path
100
+ locale, original = self.disassemble(path)
101
+
102
+ # Translate from original
103
+ tmp_uri = URI.parse(original)
104
+ tmp_path = Dibbler.slug_model.original_to_translation((locale ? locale : I18n.default_locale), tmp_uri.path)
105
+ if tmp_path
106
+ tmp_uri.path = tmp_path
107
+ translation = tmp_uri.to_s
108
+ else
109
+ translation = original
110
+ end
111
+
112
+ # Add locale if defined
113
+ translation = self.assemble(locale, translation)
114
+
115
+ return translation
116
+ end
117
+
118
+ # *************************************************************************
119
+ # Configuration
120
+ # *************************************************************************
121
+
122
+ # Default way to setup module
123
+ def self.setup
124
+ yield self
125
+ end
126
+
127
+ # *************************************************************************
128
+ # Config options
129
+ # *************************************************************************
130
+
131
+ # Disable unaccent extension in SQL queries
132
+ mattr_accessor :disable_unaccent
133
+ @@disable_unaccent = false
134
+
135
+ # Slug model
136
+ mattr_accessor :slug_model
137
+
138
+ def self.slug_model
139
+ return @@slug_model.constantize
140
+ end
141
+
142
+ @@slug_model = "Dibbler::Slug"
143
+
144
+ # Enable slugs subsystem
145
+ mattr_accessor :enable_slugs
146
+ @@enable_slugs = true
147
+
148
+ # Enable localization subsystem
149
+ mattr_accessor :enable_locales
150
+ @@enable_locales = true
151
+
152
+ # Default locale is hidden in URL by default. This feature can be disabled
153
+ # by setting true to this option.
154
+ #
155
+ # Example: Default locale is "cs" and all available locales are "cs" and
156
+ # "en". For false value URLs looks like this:
157
+ # - /fotogalerie
158
+ # - /en/photogallery
159
+ # For true value URLs looks like this:
160
+ # - /cs/fotogalerie
161
+ # - /en/photogallery
162
+ mattr_accessor :disable_default_locale
163
+ @@disable_default_locale = false
164
+
165
+ # By default, headers sent from browser are checked and default locale is
166
+ # set automatically according to browser language (if possible). This
167
+ # feature can be disable by setting true to this option.
168
+ mattr_accessor :disable_browser_locale
169
+ @@disable_browser_locale = false
170
+
171
+ # Use filter column in slug records?
172
+ #
173
+ # Filter column can be used for filtering subset of slugs valid for some
174
+ # specific application variant (different domain or subdomain) in case
175
+ # these variants share database.
176
+ mattr_accessor :use_filter
177
+ @@use_filter = false
178
+
179
+ # Use this string to filter slugs valid for this application
180
+ mattr_accessor :current_app_filter
181
+ @@current_app_filter = nil
182
+
183
+ # Map of available filters => URL to be created. Used for linking to
184
+ # different application variant (different domain).
185
+ mattr_accessor :available_filter_urls
186
+ @@available_filter_urls = {}
187
+
188
+ # If enabled translations are downcased before lookup. It means that
189
+ # translated URLs like /fotogalerie, /Fotogalerie and /FOTOGALERIE points
190
+ # to the same original URL.
191
+ mattr_accessor :downcase_translations
192
+ @@downcase_translations = false
193
+
194
+ # Slugs which are by loaded to translation tables by default without necessity
195
+ # to generate it in generators.
196
+ mattr_accessor :static_slugs
197
+ @@static_slugs = []
198
+
199
+ end
@@ -0,0 +1,17 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Engine
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 9. 6. 2015
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ module Dibbler
13
+ class Engine < ::Rails::Engine
14
+ isolate_namespace Dibbler
15
+ end
16
+ end
17
+
@@ -0,0 +1,26 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * URL helper
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 22. 7. 2015
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ module Dibbler
13
+ module Helpers
14
+ module UrlHelper
15
+
16
+ def localify(path)
17
+ return Dibbler.localify(path)
18
+ end
19
+
20
+ def slugify(path)
21
+ return Dibbler.slugify(path)
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,62 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Recognize locale based on request
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 22. 7. 2015
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ module Dibbler
13
+ module Middlewares
14
+ class Locale
15
+
16
+ def initialize(app)
17
+ @app = app
18
+ end
19
+
20
+ def call(env)
21
+ if filter(env)
22
+ @app.call(env)
23
+ else
24
+
25
+ # Match locale from path
26
+ path_locale, path = Dibbler.disassemble(env["PATH_INFO"])
27
+ path_locale = nil unless I18n.available_locales.include?(path_locale)
28
+
29
+ # Match locale from browser
30
+ if !Dibbler.disable_browser_locale && env['HTTP_ACCEPT_LANGUAGE']
31
+ browser_locale = env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
32
+ if browser_locale
33
+ browser_locale = browser_locale.to_sym
34
+ browser_locale = nil unless I18n.available_locales.include?(browser_locale)
35
+ else
36
+ browser_locale = nil
37
+ end
38
+ else
39
+ browser_locale = nil
40
+ end
41
+
42
+ # Set as default locale (for paths without locale spec)
43
+ I18n.default_locale = browser_locale if browser_locale
44
+
45
+ # Set correct locale and define default locale for URLs
46
+ I18n.locale = path_locale || I18n.default_locale
47
+ Rails.application.routes.default_url_options[:locale] = (I18n.default_locale == I18n.locale ? nil : I18n.locale)
48
+
49
+ @app.call(env)
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ def filter(env)
56
+ return true if env["PATH_INFO"].start_with?("/assets/")
57
+ false
58
+ end
59
+
60
+ end
61
+ end
62
+ end