seo_sensei 0.0.0.1 → 0.0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4559e94410d5adb7fe6835221c337519d57a65725efcf1397561508c4641bde
4
- data.tar.gz: 4f23546fc9de3a56e4b829b2868e1447b0eeac2d0e95343fdd31eddaecbd4af7
3
+ metadata.gz: acfe5bb9d4f7612b10374ad40fd84af8d332cbd52d576c75f58d899a8e88e21a
4
+ data.tar.gz: e4ec71cd3597227fa95ff8e8442be66b2f06ca15ec6de680b2d2e281b690c88f
5
5
  SHA512:
6
- metadata.gz: 600a8ccb45a3cbd90bf0a51a1a645df99d7c86636407c1359ef204032cedd0aaf12cb4cfb11c0f5a72b7eab0ed8f565332ae0e8d761e703cc5cc4b8664239276
7
- data.tar.gz: ed47f595763f879e1ef9ce01e30a39d0eb2adf8f3b015ce383d76983440e5c0ac441feae5da2d9a1315e71c5b81b1e707a1ca44d3a1e753d1a4cadec124d7ab3
6
+ metadata.gz: 88c8ebf9ab0b6ff93de5720d3d5725ed298d7854ca4a6d0b16187563d7c4cec23b14e512079b499b005b896f8880cfc2b24466773759f71042b6ed20e4a081e1
7
+ data.tar.gz: 488a4254c5999edee4e3584ac3d282c80b78089fc6bf515d595af9bc2c051685d861baeacdca242869adcdda082990a2b79f7f6a9d9f33322a1061ba5edf9e03
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ module SeoSensei
6
+ module Generators
7
+ class GenerateTranslationsGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ desc ""
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ module SeoSensei
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ desc "Creates a SeoSensei initializer and copy locale file to your application."
11
+
12
+ def copy_initializer
13
+ template "seo_sensei.rb", "config/initializers/seo_sensei.rb"
14
+ end
15
+
16
+ def copy_migration
17
+ template "migration.rb", "db/migrate/#{Time.now.strftime('%Y%m%d%H%M')}_create_seo_sensei.rb", migration_version: migration_version
18
+ end
19
+
20
+ def copy_locale
21
+ copy_file "../../../config/locales/en.yml", "config/locales/seo_sensei.en.yml"
22
+ end
23
+
24
+ private
25
+
26
+ def rails5_and_up?
27
+ Rails::VERSION::MAJOR >= 5
28
+ end
29
+
30
+ def migration_version
31
+ if rails5_and_up?
32
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateSeoSensei < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :seo_tags do |t|
6
+ t.references :seoable, polymorphic: true
7
+ t.string :title
8
+ t.string :keywords
9
+ t.string :description
10
+
11
+ t.timestamps null: false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ SeoSensei.configure do |config|
2
+ config.disable_seo = Rails.env.test?
3
+ config.translation_key = :seo
4
+ config.disable_database_lookup = false
5
+ end
@@ -9,21 +9,17 @@ module SeoSensei
9
9
  def enable_seo(options = {})
10
10
  before_action(options) do
11
11
  resource_name = options.delete(:for) || controller_name
12
- translated_seo = I18n.t("seo.#{resource_name}.#{action_name}", default: {})
13
- set_meta_tags(translated_seo) if translated_seo.present?
12
+ if (translated_seo = ::SeoSensei::Lookup.call(controller_name: resource_name, action_name: action_name))
13
+ set_meta_tags(translated_seo)
14
+ end
14
15
  end
15
16
  end
16
17
  end
17
18
 
18
19
  def seo_tags_with(resource)
19
- attributes = resource.kind_of?(Hash) ? resource : resource.attributes
20
- translated_seo = I18n.t("seo.#{controller_name}.#{action_name}", default: {})
21
- seo_attributes = translated_seo.keys.inject({}) do |h, k|
22
- h[k] = I18n.t("seo.#{controller_name}.#{action_name}.#{k.to_s}", attributes.symbolize_keys)
23
- h
20
+ if (translated_seo = ::SeoSensei::Lookup.call(controller_name: controller_name, action_name: action_name, resource: resource))
21
+ set_meta_tags(translated_seo)
24
22
  end
25
-
26
- set_meta_tags(seo_attributes) if seo_attributes.present?
27
23
  end
28
24
  end
29
25
  end
@@ -0,0 +1,16 @@
1
+ require 'seo_sensei/lookups/database'
2
+ require 'seo_sensei/lookups/translation_attributes'
3
+ require 'seo_sensei/lookups/translation'
4
+ require 'seo_sensei/lookups/default_translation'
5
+
6
+ module SeoSensei
7
+ class Lookup
8
+ def self.call(controller_name:, action_name:, resource: nil)
9
+ lookup = ::SeoSensei::Lookups::Database.call(resource) ||
10
+ ::SeoSensei::Lookups::Translation.call(controller_name, action_name, resource) ||
11
+ ::SeoSensei::Lookups::DefaultTranslation.call
12
+
13
+ lookup.to_h.presence
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ module SeoSensei
2
+ module Lookups
3
+ class Database
4
+ def self.call(resource)
5
+ return if SeoSensei.configuration.disable_database_lookup
6
+ return if resource.blank? || resource.is_a?(Hash)
7
+
8
+ ::SeoSensei::Models::SeoTag.find_by(seoable: resource)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module SeoSensei
2
+ module Lookups
3
+ class DefaultTranslation
4
+ def self.call
5
+ translation_key = [
6
+ SeoSensei.configuration.translation_key.to_s,
7
+ 'default'
8
+ ].join('.')
9
+
10
+ return unless I18n.exists?(translation_key)
11
+
12
+ I18n.t(translation_key)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module SeoSensei
2
+ module Lookups
3
+ class Translation
4
+ def self.call(controller_name, action_name, resource)
5
+ translation_key = [
6
+ SeoSensei.configuration.translation_key.to_s,
7
+ controller_name,
8
+ action_name
9
+ ].join('.')
10
+
11
+ return unless I18n.exists?(translation_key)
12
+
13
+ translations = {}
14
+ translation_attributes = ::SeoSensei::Lookups::TranslationAttributes.for(resource)
15
+
16
+ I18n.t(translation_key).keys.each do |key|
17
+ translations[key] = I18n.t("#{translation_key}.#{key}", translation_attributes.symbolize_keys)
18
+ end
19
+
20
+ translations
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module SeoSensei
2
+ module Lookups
3
+ class TranslationAttributes
4
+ def self.for(resource)
5
+ if resource.respond_to?(:attributes)
6
+ resource.attributes
7
+ elsif resource.is_a?(Hash)
8
+ resource
9
+ else
10
+ {}
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module SeoSensei
2
+ module Models
3
+ class SeoTag < ::ActiveRecord::Base
4
+ belongs_to :seoable, polymorphic: true
5
+
6
+ def to_h
7
+ slice("title", "keywords", "description")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -6,7 +6,7 @@ module SeoSensei
6
6
  #
7
7
  # @return [String]
8
8
  def to_s
9
- "0.0.0.1"
9
+ "0.0.0.2"
10
10
  end
11
11
  end
12
12
  end
data/lib/seo_sensei.rb CHANGED
@@ -1,8 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'meta-tags'
4
+ require 'seo_sensei/lookup'
4
5
  require 'seo_sensei/controllers/helpers'
6
+ require 'seo_sensei/models/seo_tag'
5
7
 
6
8
  ActiveSupport.on_load(:action_controller) do
7
9
  include ::SeoSensei::Controllers::Helpers
8
10
  end
11
+
12
+ module SeoSensei
13
+ class << self
14
+ attr_accessor :configuration
15
+ end
16
+
17
+ def self.configure
18
+ self.configuration ||= Configuration.new
19
+ yield(configuration)
20
+ end
21
+
22
+ class Configuration
23
+ attr_accessor :translation_key, :disable_seo, :disable_database_lookup
24
+
25
+ def initialize
26
+ @disable_database_lookup = false
27
+ @disable_seo = true
28
+ @translation_key = :seo
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seo_sensei
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.1
4
+ version: 0.0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Dąbrowski
@@ -78,8 +78,18 @@ executables: []
78
78
  extensions: []
79
79
  extra_rdoc_files: []
80
80
  files:
81
+ - lib/generators/seo_sensei/generate_translations_generator.rb
82
+ - lib/generators/seo_sensei/install_generator.rb
83
+ - lib/generators/templates/migration.rb
84
+ - lib/generators/templates/seo_sensei.rb
81
85
  - lib/seo_sensei.rb
82
86
  - lib/seo_sensei/controllers/helpers.rb
87
+ - lib/seo_sensei/lookup.rb
88
+ - lib/seo_sensei/lookups/database.rb
89
+ - lib/seo_sensei/lookups/default_translation.rb
90
+ - lib/seo_sensei/lookups/translation.rb
91
+ - lib/seo_sensei/lookups/translation_attributes.rb
92
+ - lib/seo_sensei/models/seo_tag.rb
83
93
  - lib/seo_sensei/version.rb
84
94
  homepage: http://github.com/rubyhero/seo_sensei
85
95
  licenses: