rack-dev-mark 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGQ4OTM0MjZiOGJkMDAxYTNhOTNmNGMzYTQwYjVjNGZlOTYzMTMzOQ==
4
+ YjFlYzc0MDk5MDA0ZGU1NDNmZTA3N2QwZWYzNTg5ZDhhNmIxMTM2YQ==
5
5
  data.tar.gz: !binary |-
6
- ZTMxN2EyYjhhZDE0YWRhYTkxYjJhNDc3MjlkMzhmMGY0YjM1ODE1YQ==
6
+ ZmU2ZTgxODMzZmY0N2ZjYjE1NDc5MmU0MmM2YzJmM2FiYjU3MjYyZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjJiMmI5N2M3Nzc1ZjhiNWZjNWY0MDZlOWJkMzkyZGIwMjNmNTU4YmY5MTYz
10
- YmUyZmY1ZTY1ZDZmMTFiZTY4ZDMxZjk0MGFhNzUwNzY5YjI1YmRjMWY0YzNk
11
- NmU4YWFjZTRmYTA5ODg5NzQ1YTFiOTVjY2E5YTUzZmYxZGU0NjA=
9
+ OTc1MTVkYjllODI0YTBmMDc5MDBhZmFkZjM1ZWE5MmUzMWE2N2VjYzIyOGRh
10
+ MWQ3ZjVhZWQ1M2NjYmM5ZGM3YzVmYTk4ZjY0Y2JjZjRlYzA0ZmI2NmI1MjQw
11
+ OTIwZWYxZmZiOGUwYzQ3YTkyMTdkOTYzNTQ5NDZkZDk1M2I4NDg=
12
12
  data.tar.gz: !binary |-
13
- NjUwOTM0ZThmYmY2ZTQ1ZTUzMGI0OGVlOWFhMjM2YjEyNmU1YThmYzJjMzY4
14
- ZTA3ZjA4MjkzMjQzMDM4NGYwYWU3ODI0YjdlMzkwOWI0MzUwODA4YWJkYTFm
15
- ZjA5ODQxMjUzYTVlYWQzOTQ5ZDBkYTgzNTRiMjdjNTUzMTc5MDE=
13
+ MjU5MDkzN2M2NzQ1NGM3OGFjNTAwZWJjMjkwYzk3OWIwN2FiNjFjYTAzMDYz
14
+ NWVmNjcyMDdhM2U3YmU5MWJjYWJhYmJjZTA0NzY2YjJhNWEyYjk3ZTU3NDc0
15
+ YTFkNmYwYzQ3YzE0NDIzNTYzMjg5YWEyYTRiNzU4ZmY3OTA3NjA=
data/.travis.yml CHANGED
@@ -14,6 +14,7 @@ env:
14
14
  - RAILS_VERSION=3.2.0
15
15
  - RAILS_VERSION=4.0.0
16
16
  - RAILS_VERSION=4.1.0
17
+ - I18N_VERSION=0.6.0
17
18
 
18
19
  script: "bundle exec rake spec"
19
20
 
data/Gemfile CHANGED
@@ -2,5 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem "rack", "~> #{ENV['RACK_VERSION']}" if ENV['RACK_VERSION'].to_s != ''
4
4
  gem "rails", "~> #{ENV['RAILS_VERSION']}" if ENV['RAILS_VERSION'].to_s != ''
5
+ gem "i18n", "~> #{ENV['I18N_VERSION']}" if ENV['I18N_VERSION'].to_s != ''
5
6
 
6
7
  gemspec
data/README.md CHANGED
@@ -6,8 +6,9 @@
6
6
  [![Coverage Status][cov-image]][cov-link]
7
7
  [![Code Climate][gpa-image]][gpa-link]
8
8
 
9
- Differentiate development environment from production.
10
- You can choose [themes](THEME.md) to differentiate the page.
9
+ Differentiate development environment from production. You can choose [themes](THEME.md) to differentiate the page.
10
+
11
+ The running sample is available [here](http://rack-dev-mark.dtaniwaki.com/).
11
12
 
12
13
  ## Screenshot
13
14
 
@@ -109,6 +110,32 @@ end
109
110
 
110
111
  [Here](COMPATIBILITY.md) is the compatibility list which many people often ask.
111
112
 
113
+ #### Custom env string
114
+
115
+ Set the custom env string maually.
116
+
117
+ ```ruby
118
+ module MyApp
119
+ class Application < Rails::Application
120
+ config.rack_dev_mark.env = 'foo'
121
+ end
122
+ end
123
+
124
+ ## I18n Support
125
+
126
+ Get i18n string with rack_dev_mark locale strings.
127
+
128
+ e.g. In `config/locale/rack_dev_mark.ja.yml`
129
+
130
+ ```
131
+ ja:
132
+ rack_dev_mark:
133
+ development: '開発中'
134
+ staging: 'ステージング'
135
+ ```
136
+
137
+ Then, you will get translated string on the pages!
138
+
112
139
  ## Custom Theme
113
140
 
114
141
  Although the default themes are `title` and `github_fork_ribbon`, you can create your own themes inheriting `Rack::DevMark::Theme::Base`.
@@ -117,14 +144,14 @@ Although the default themes are `title` and `github_fork_ribbon`, you can create
117
144
  require 'rack/dev-mark/theme/base'
118
145
 
119
146
  class NewTheme < Rack::DevMark::Theme::Base
120
- def insert_into(html)
147
+ def insert_into(html, env, params = {})
121
148
  # Do something for your theme
122
149
  html
123
150
  end
124
151
  end
125
152
 
126
153
  class AnotherTheme < Rack::DevMark::Theme::Base
127
- def insert_into(html)
154
+ def insert_into(html, env, params = {})
128
155
  # Do something for your theme
129
156
  html
130
157
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.4
1
+ 0.7.0
@@ -11,8 +11,13 @@ module Rack
11
11
  insert_into_file target_path, after: "< Rails::Application\n" do <<-EOS
12
12
  # Enable rack-dev-mark
13
13
  config.rack_dev_mark.enable = !Rails.env.production?
14
+ #
15
+ # Customize the env string (default Rails.env)
16
+ # config.rack_dev_mark.env = 'foo'
17
+ #
14
18
  # Customize themes if you want to do so
15
19
  # config.rack_dev_mark.theme = [:title, :github_fork_ribbon]
20
+ #
16
21
  # Customize inserted place of the middleware if necessary.
17
22
  # You can use either `insert_before` or `insert_after`
18
23
  # config.rack_dev_mark.insert_before SomeOtherMiddleware
data/lib/rack/dev-mark.rb CHANGED
@@ -37,6 +37,11 @@ module Rack
37
37
  s.to_s == '' ? nil : s
38
38
  end
39
39
  end
40
+
41
+ if defined?(::I18n)
42
+ require 'rack/dev-mark/i18n_helper'
43
+ include I18nHelper
44
+ end
40
45
  end
41
46
  end
42
47
 
@@ -0,0 +1,18 @@
1
+ require 'i18n'
2
+
3
+ module Rack
4
+ module DevMark
5
+ module I18nHelper
6
+ def self.included(base)
7
+ class << base
8
+ def env_with_i18n
9
+ s = env_without_i18n
10
+ ::I18n.translate(s, scope: 'rack_dev_mark', default: s)
11
+ end
12
+ alias_method :env_without_i18n, :env
13
+ alias_method :env, :env_with_i18n
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -7,10 +7,10 @@ module Rack
7
7
  def initialize(app, themes = [:title, :github_fork_ribbon])
8
8
  @app = app
9
9
  @themes = [themes].flatten.map do |theme|
10
- theme = theme.is_a?(Symbol) ? Rack::DevMark::Theme.const_get(camelize(theme.to_s)).new : theme
11
- theme.setup Rack::DevMark.env, Rack::DevMark.revision, Rack::DevMark.timestamp
12
- theme
10
+ theme.is_a?(Symbol) ? Rack::DevMark::Theme.const_get(camelize(theme.to_s)).new : theme
13
11
  end
12
+ @revision = Rack::DevMark.revision
13
+ @timestamp = Rack::DevMark.timestamp
14
14
  end
15
15
 
16
16
  def call(env)
@@ -41,7 +41,7 @@ module Rack
41
41
 
42
42
  def insert_dev_marks(body)
43
43
  @themes.each do |theme|
44
- body = theme.insert_into(body)
44
+ body = theme.insert_into(body, Rack::DevMark.env, revision: @revision, timestamp: @timestamp)
45
45
  end
46
46
  body
47
47
  end
@@ -1,7 +1,7 @@
1
1
  module Rack
2
2
  module DevMark
3
3
  class RailsOptions
4
- attr_accessor :enable, :theme
4
+ attr_accessor :env, :enable, :theme
5
5
 
6
6
  alias_method :custom_theme, :theme
7
7
  alias_method :custom_theme=, :theme=
@@ -6,7 +6,7 @@ module Rack
6
6
  class Railtie < Rails::Railtie
7
7
  config.rack_dev_mark = RailsOptions.new
8
8
 
9
- initializer "rack-dev-mark.configure_rails_initialization" do |app|
9
+ initializer "rack-dev-mark.insert_middleware" do |app|
10
10
  if app.config.rack_dev_mark.enable || Rack::DevMark.rack_dev_mark_env
11
11
  racks = []
12
12
 
@@ -22,6 +22,12 @@ module Rack
22
22
  app.config.app_middleware.send(insert_method, *racks)
23
23
  end
24
24
  end
25
+
26
+ initializer "rack-dev-mark.set_env", after: "rack-dev-mark.insert_middleware" do |app|
27
+ if app.config.rack_dev_mark.enable || Rack::DevMark.rack_dev_mark_env
28
+ Rack::DevMark.env = app.config.rack_dev_mark.env
29
+ end
30
+ end
25
31
  end
26
32
  end
27
33
  end
@@ -2,19 +2,12 @@ module Rack
2
2
  module DevMark
3
3
  module Theme
4
4
  class Base
5
- attr_reader :env, :revision, :timestamp
6
-
7
- def initialize(*args)
5
+ def initialize(options = {})
8
6
  raise RuntimeError, 'Abstract class can not be instantiated' if self.class == Rack::DevMark::Theme::Base
7
+ @options = options
9
8
  end
10
9
 
11
- def setup(env, revision, timestamp)
12
- @env = env
13
- @revision = revision
14
- @timestamp = timestamp
15
- end
16
-
17
- def insert_into(html)
10
+ def insert_into(html, env, params = {})
18
11
 
19
12
  end
20
13
 
@@ -5,12 +5,10 @@ module Rack
5
5
  module DevMark
6
6
  module Theme
7
7
  class GithubForkRibbon < Base
8
- def initialize(options = {})
9
- @options = options
10
- super
11
- end
8
+ def insert_into(html, env, params = {})
9
+ revision = params[:revision]
10
+ timestamp = params[:timestamp]
12
11
 
13
- def insert_into(html)
14
12
  position = @options[:position] || 'left'
15
13
  color = @options[:color] || 'red'
16
14
  fixed = @options[:fixed] ? ' fixed' : ''
@@ -18,6 +16,7 @@ module Rack
18
16
  title << revision if revision.to_s != ''
19
17
  title << timestamp if timestamp.to_s != ''
20
18
  title = title.join("&#10;")
19
+
21
20
  s = <<-EOS
22
21
  #{stylesheet_link_tag "github-fork-ribbon-css/gh-fork-ribbon.css"}
23
22
  <!--[if lt IE 9]>
@@ -25,6 +24,7 @@ module Rack
25
24
  <![endif]-->
26
25
  <div class="github-fork-ribbon-wrapper #{position}#{fixed}" onClick="this.style.display='none'" title="#{title}"><div class="github-fork-ribbon #{color}"><span class="github-fork-ribbon-text">#{env}</span></div></div>
27
26
  EOS
27
+
28
28
  html.sub %r{(<body[^>]*>)}i, "\\1#{s.strip}"
29
29
  end
30
30
  end
@@ -4,24 +4,19 @@ module Rack
4
4
  module DevMark
5
5
  module Theme
6
6
  class Tag < Base
7
- def initialize(options = {})
8
- @options = options
9
- super
10
- end
11
-
12
- def insert_into(html)
7
+ def insert_into(html, env, params = {})
13
8
  name = @options[:name]
14
9
 
15
10
  if name
16
11
  html = gsub_tag_content html, name do |value|
17
- env_with(value)
12
+ env_with_value(env, value)
18
13
  end
19
14
  end
20
15
 
21
16
  if attribute = @options[:attribute]
22
17
  Array(attribute).each do |attr|
23
18
  html = gsub_tag_attribute html, name, attr do |value|
24
- env_with(value)
19
+ env_with_value(env, value)
25
20
  end
26
21
  end
27
22
  end
@@ -31,7 +26,7 @@ module Rack
31
26
 
32
27
  private
33
28
 
34
- def env_with(org)
29
+ def env_with_value(env, org)
35
30
  s = env.to_s
36
31
  s = s.upcase if @options[:upcase]
37
32
 
@@ -32,8 +32,13 @@ module MyApp
32
32
  class Application < Rails::Application
33
33
  # Enable rack-dev-mark
34
34
  config.rack_dev_mark.enable = !Rails.env.production?
35
+ #
36
+ # Customize the env string (default Rails.env)
37
+ # config.rack_dev_mark.env = 'foo'
38
+ #
35
39
  # Customize themes if you want to do so
36
40
  # config.rack_dev_mark.theme = [:title, :github_fork_ribbon]
41
+ #
37
42
  # Customize inserted place of the middleware if necessary.
38
43
  # You can use either `insert_before` or `insert_after`
39
44
  # config.rack_dev_mark.insert_before SomeOtherMiddleware
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ if defined?(::I18n)
5
+ describe Rack::DevMark::I18nHelper do
6
+ let(:translations) {
7
+ {
8
+ ja: {
9
+ rack_dev_mark: {
10
+ test: 'テスト'
11
+ }
12
+ }
13
+ }
14
+ }
15
+ before do
16
+ ::I18n.locale = :ja
17
+ ::I18n.backend.store_translations(:ja, translations[:ja])
18
+ end
19
+ after do
20
+ ::I18n.backend.store_translations(:ja, {})
21
+ ::I18n.locale = :en
22
+ end
23
+ it "returns i18n string" do
24
+ Rack::DevMark.env = 'test'
25
+ expect(Rack::DevMark.env).to eq('テスト')
26
+ end
27
+ context "without matched key" do
28
+ it "returns original string" do
29
+ Rack::DevMark.env = 'foo'
30
+ expect(Rack::DevMark.env).to eq('foo')
31
+ end
32
+ end
33
+ end
34
+ end
@@ -6,6 +6,8 @@ describe Rack::DevMark::RailsOptions do
6
6
  it do
7
7
  is_expected.to respond_to(:enable)
8
8
  is_expected.to respond_to(:enable=)
9
+ is_expected.to respond_to(:env)
10
+ is_expected.to respond_to(:env=)
9
11
  is_expected.to respond_to(:theme)
10
12
  is_expected.to respond_to(:theme=)
11
13
  is_expected.to respond_to(:insert_type)
@@ -39,7 +39,7 @@ describe Rack::DevMark::Railtie do
39
39
  end
40
40
  end
41
41
  context "rack_dev_mark theme" do
42
- let(:theme) { d = double setup: nil; allow(d).to receive(:insert_into){ |b| "#{b} dev-mark" }; d }
42
+ let(:theme) { d = double setup: nil; allow(d).to receive(:insert_into){ |b, _, _| "#{b} dev-mark" }; d }
43
43
  before do
44
44
  @app.config.rack_dev_mark.enable = true
45
45
  @app.config.rack_dev_mark.theme = [theme]
@@ -50,7 +50,7 @@ describe Rack::DevMark::Railtie do
50
50
  end
51
51
  end
52
52
  context "rack_dev_mark custom_theme alias" do
53
- let(:theme) { d = double setup: nil; allow(d).to receive(:insert_into){ |b| "#{b} dev-mark" }; d }
53
+ let(:theme) { d = double setup: nil; allow(d).to receive(:insert_into){ |b, _, _| "#{b} dev-mark" }; d }
54
54
  before do
55
55
  @app.config.rack_dev_mark.enable = true
56
56
  @app.config.rack_dev_mark.custom_theme = [theme]
@@ -90,5 +90,15 @@ describe Rack::DevMark::Railtie do
90
90
  expect(middlewares.index(Rack::DevMark::Middleware)).to eq(middlewares.index(dummy_middleware) + 1)
91
91
  end
92
92
  end
93
+ context "rack_dev_mark env" do
94
+ before do
95
+ @app.config.rack_dev_mark.enable = true
96
+ @app.config.rack_dev_mark.env = 'something'
97
+ end
98
+ it 'sets the env string' do
99
+ @app.initialize!
100
+ expect(Rack::DevMark.env).to eq('something')
101
+ end
102
+ end
93
103
  end
94
104
  end
@@ -8,11 +8,6 @@ describe Rack::DevMark::Theme::Base do
8
8
  end
9
9
  describe "subclass" do
10
10
  subject { Class.new(Rack::DevMark::Theme::Base).new }
11
- it "sets up" do
12
- subject.setup 'env', 'rev', 'time'
13
- expect(subject.env).to eq('env')
14
- expect(subject.revision).to eq('rev')
15
- end
16
11
  describe "#gsub_tag_content" do
17
12
  let(:input) { %Q|<body><h1>head</h1><a href="something">x</a><span></span>y</div>| }
18
13
  let(:output) { %Q|<body><h1>head</h1><a href="something">replaced</a><span></span>y</div>| }
@@ -10,29 +10,29 @@ describe Rack::DevMark::Theme::Tag do
10
10
  # No insert
11
11
  end
12
12
 
13
- describe "#env_with" do
13
+ describe "#env_with_value" do
14
14
  context "upcase is true" do
15
15
  let(:options) { {upcase: true} }
16
16
  it "replace a string" do
17
- expect(subject.send(:env_with, 'foo')).to eq('(ENV) foo')
17
+ expect(subject.send(:env_with_value, 'env', 'foo')).to eq('(ENV) foo')
18
18
  end
19
19
  end
20
20
  context "upcase is false" do
21
21
  let(:options) { {upcase: false} }
22
22
  it "replace a string" do
23
- expect(subject.send(:env_with, 'foo')).to eq('(env) foo')
23
+ expect(subject.send(:env_with_value, 'env', 'foo')).to eq('(env) foo')
24
24
  end
25
25
  end
26
26
  context "type is prefix" do
27
27
  let(:options) { {type: 'prefix'} }
28
28
  it "replace a string" do
29
- expect(subject.send(:env_with, 'foo')).to eq('(env) foo')
29
+ expect(subject.send(:env_with_value, 'env', 'foo')).to eq('(env) foo')
30
30
  end
31
31
  end
32
32
  context "type is postfix" do
33
33
  let(:options) { {type: 'postfix'} }
34
34
  it "replace a string" do
35
- expect(subject.send(:env_with, 'foo')).to eq('foo (env)')
35
+ expect(subject.send(:env_with_value, 'env', 'foo')).to eq('foo (env)')
36
36
  end
37
37
  end
38
38
  end
@@ -2,15 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe Rack::DevMark do
4
4
  subject { Rack::DevMark }
5
- before do
6
- @rack_env = ENV['RACK_ENV']
7
- @rails_env = ENV['RAILS_ENV']
8
- end
9
- after do
10
- ENV['RACK_ENV'] = @rack_env
11
- ENV['RAILS_ENV'] = @rails_env
12
- subject.instance_variable_set("@env", nil)
13
- end
14
5
  describe "::env" do
15
6
  it "returns development as default" do
16
7
  ENV['RAILS_ENV'] = nil
data/spec/spec_helper.rb CHANGED
@@ -26,10 +26,26 @@ begin
26
26
  $stdout.write "* Rails has been loaded.\n"
27
27
  rescue LoadError
28
28
  end
29
+ begin
30
+ require 'i18n'
31
+ $stdout.write "* I18n has been loaded.\n"
32
+ I18n.enforce_available_locales = true
33
+ I18n.available_locales = [:ja, :en]
34
+ rescue LoadError
35
+ end
29
36
  require 'rack-dev-mark'
30
37
 
31
38
  Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
32
39
 
33
40
  RSpec.configure do |config|
41
+ config.before :each do
42
+ @rack_env = ENV['RACK_ENV']
43
+ @rails_env = ENV['RAILS_ENV']
44
+ end
45
+ config.after :each do
46
+ ENV['RACK_ENV'] = @rack_env
47
+ ENV['RAILS_ENV'] = @rails_env
48
+ Rack::DevMark.env = nil
49
+ end
34
50
  end
35
51
 
@@ -2,16 +2,12 @@ RSpec.shared_context "theme context" do
2
2
  def read_stylesheet(path)
3
3
  ::File.open(::File.join(::File.dirname(__FILE__), '../../vendor/assets/stylesheets', path)).read
4
4
  end
5
-
6
- before do
7
- subject.setup 'env', 'rev', 'time'
8
- end
9
5
  end
10
6
 
11
7
  RSpec.shared_examples "theme" do
12
8
  let (:src) { %Q~<html><head>head<title>title</title></head><body>body</body></html>~ }
13
9
  let (:out) { %Q~<html><head>head<title>title</title></head><body>body</body></html>~ }
14
10
  it "inserts env mark" do
15
- expect(subject.insert_into(src)).to eq(out)
11
+ expect(subject.insert_into(src, 'env', revision: 'rev', timestamp: 'time')).to eq(out)
16
12
  end
17
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-dev-mark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Taniwaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-14 00:00:00.000000000 Z
11
+ date: 2014-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -86,6 +86,7 @@ files:
86
86
  - lib/rack-dev-mark.rb
87
87
  - lib/rack/dev-mark.rb
88
88
  - lib/rack/dev-mark/error.rb
89
+ - lib/rack/dev-mark/i18n_helper.rb
89
90
  - lib/rack/dev-mark/middleware.rb
90
91
  - lib/rack/dev-mark/rails_options.rb
91
92
  - lib/rack/dev-mark/railtie.rb
@@ -104,6 +105,7 @@ files:
104
105
  - misc/theme/title.png
105
106
  - rack-dev-mark.gemspec
106
107
  - spec/generators/rack/dev-mark/install_generator_spec.rb
108
+ - spec/rack/dev-mark/i18n_helper_spec.rb
107
109
  - spec/rack/dev-mark/middleware_spec.rb
108
110
  - spec/rack/dev-mark/rails_options_spec.rb
109
111
  - spec/rack/dev-mark/railtie_spec.rb
@@ -144,6 +146,7 @@ specification_version: 4
144
146
  summary: Differentiate development environment from production
145
147
  test_files:
146
148
  - spec/generators/rack/dev-mark/install_generator_spec.rb
149
+ - spec/rack/dev-mark/i18n_helper_spec.rb
147
150
  - spec/rack/dev-mark/middleware_spec.rb
148
151
  - spec/rack/dev-mark/rails_options_spec.rb
149
152
  - spec/rack/dev-mark/railtie_spec.rb