user_switcher 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3387dcbd68cff720cf1b0394964830ce1ad5ea35768f713e16a2cde6287600bb
4
- data.tar.gz: 01a79572bdc05f16eda5b9a6d0f4a87220491b30597e579cac2d3c1ca2975d45
3
+ metadata.gz: a5ebb17d7d0453866c0f74c182403298555cfdb2fd5dd301c2dede6368bf028a
4
+ data.tar.gz: 3fe71ea3d7ed7c5a0b7af6f68e081804675b66509354cc7903a7823080d4771f
5
5
  SHA512:
6
- metadata.gz: 989d36c1585d9e63abe5b74e239e58c451f5978a4fa2b1e1e63c294bd72a0a7fde8dd2dd8ddf47811dcc28e26e18935b388b53e7dc5700662e706a12232ad2fd
7
- data.tar.gz: b93a782c0d35c6f53763edc9168a5230dba4d800f1b3f67e2f2ab3881c962188330a161e8d0fd3aeac49e3dca6e6df30cb17288658bcdc63219d2c5e11d88168
6
+ metadata.gz: 232cda7d8dd162df723e7746fa0b128c66ddf0c73233bf40ea5c62e8c13b52848f0dc67c29a64936fb06b782fc0005965833bb60b0d48f65947d656c91aa15b8
7
+ data.tar.gz: 76bf8a37d560402d73e7553237fea7bbe28dceafee499292745f4e9fbbc3c4e319af07b3f8271ace8eb1681c4e4b19f1f1d4ad27fcde64a6bb216c55a5269f34
@@ -4,6 +4,7 @@ module UserSwitcher
4
4
  if defined?(Sorcery)
5
5
  skip_before_action :require_login, raise: false
6
6
  end
7
+
7
8
  before_action :prepare_login_params
8
9
 
9
10
  def create
@@ -13,7 +14,7 @@ module UserSwitcher
13
14
  private
14
15
 
15
16
  def prepare_login_params
16
- user_param = params[:users].find { |param| param[:id] == params[:user_id] }
17
+ user_param = (params[:users] || [{}]).find { |user| user[:id] == params[:user_id] }
17
18
  return if user_param.blank?
18
19
 
19
20
  params[:id] = user_param[:id]
data/config/routes.rb CHANGED
@@ -1,3 +1,7 @@
1
- UserSwitcher::Engine.routes.draw do
2
- post "login", to: "user_sessions#create"
1
+ Rails.application.routes.draw do
2
+ if UserSwitcher.enabled?
3
+ scope UserSwitcher.config.routes_prefix, module: "user_switcher" do
4
+ post "login", to: "user_sessions#create", as: "user_switcher_login"
5
+ end
6
+ end
3
7
  end
@@ -7,15 +7,25 @@ module UserSwitcher
7
7
  def config
8
8
  @_config ||= Config.new
9
9
  end
10
+
11
+ def enabled?
12
+ !!config.enabled
13
+ end
10
14
  end
11
15
 
12
16
  class Config
13
- attr_accessor :login_url, :login_procedure
17
+ attr_accessor :login_url, :login_procedure, :enabled, :routes_prefix
14
18
 
15
19
  def initialize
16
20
  @login_url = "/user_switcher/login"
17
21
  @users = load_users_from_file
18
22
  @login_procedure = default_login_procedure
23
+ @routes_prefix = "user_switcher"
24
+ @enabled = if defined?(Rails)
25
+ Rails.env.development?
26
+ else
27
+ false
28
+ end
19
29
  end
20
30
 
21
31
  def default_login_procedure
@@ -9,7 +9,6 @@ module UserSwitcher::Middlewares
9
9
 
10
10
  @users = config[:users] || []
11
11
  @login_url = config[:login_url]
12
- @form_erb = config[:form_erb] || load_form_erb
13
12
  end
14
13
 
15
14
  def call(env)
@@ -25,10 +24,10 @@ module UserSwitcher::Middlewares
25
24
  when Array
26
25
  body = body[0]
27
26
  end
28
-
29
27
  body = body.dup if body.frozen?
28
+
30
29
  new_body = insert_form(body)
31
- new_body = insert_style(new_body)
30
+ new_body = insert_inner_head(new_body)
32
31
  headers['Content-Length'] &&= new_body.bytesize.to_s
33
32
 
34
33
  [status, headers, [new_body]]
@@ -43,20 +42,24 @@ module UserSwitcher::Middlewares
43
42
  html.gsub %r{<body(.*?)>(.*)<\/body>}mi, '<body\1>' + form + '\2</body>'
44
43
  end
45
44
 
46
- def insert_style(html)
47
- html.sub %r{<head(.*?)>(.*)<\/head>}mi, '<head\1>\2' + style + '</head>'
45
+ def insert_inner_head(html)
46
+ html.sub %r{<head(.*?)>(.*)<\/head>}mi, '<head\1>\2' + head + '</head>'
48
47
  end
49
48
 
50
49
  def form
51
- @form ||= ERB.new(@form_erb).result(binding)
50
+ @form ||= ERB.new(load_form_erb).result(binding)
51
+ end
52
+
53
+ def head
54
+ @head ||= ERB.new(load_head_erb).result(binding)
52
55
  end
53
56
 
54
- def style
55
- @style ||= File.read(File.expand_path('../views/style.html', __dir__))
57
+ def load_head_erb
58
+ File.read(File.expand_path('../views/head.html.erb', __dir__))
56
59
  end
57
60
 
58
61
  def load_form_erb
59
- File.read(File.expand_path('../views/form.erb', __dir__))
62
+ File.read(File.expand_path('../views/form.html.erb', __dir__))
60
63
  end
61
64
  end
62
65
  end
@@ -1,5 +1,7 @@
1
1
  module UserSwitcher
2
2
  def self.initialize!(app)
3
+ return if !UserSwitcher.enabled?
4
+
3
5
  app.middleware.insert(-1, UserSwitcher::Middlewares::SwitcherInserter, UserSwitcher.config.to_middleware_config)
4
6
  end
5
7
 
@@ -1,3 +1,3 @@
1
1
  module UserSwitcher
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
File without changes
@@ -19,5 +19,4 @@
19
19
  #user_switcher_menu input {
20
20
  margin: 0; padding: 0;
21
21
  }
22
-
23
22
  </style>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: user_switcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ykpythemind
@@ -72,8 +72,8 @@ files:
72
72
  - lib/user_switcher/middlewares/switcher_inserter.rb
73
73
  - lib/user_switcher/railtie.rb
74
74
  - lib/user_switcher/version.rb
75
- - lib/user_switcher/views/form.erb
76
- - lib/user_switcher/views/style.html
75
+ - lib/user_switcher/views/form.html.erb
76
+ - lib/user_switcher/views/head.html.erb
77
77
  homepage: https://github.com/ykpythemind/user_switcher
78
78
  licenses:
79
79
  - MIT