bizside 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/app/assets/images/jquery-treeTable/images/toggle-collapse-dark.png +0 -0
- data/app/assets/images/jquery-treeTable/images/toggle-collapse-light.png +0 -0
- data/app/assets/images/jquery-treeTable/images/toggle-expand-dark.png +0 -0
- data/app/assets/images/jquery-treeTable/images/toggle-expand-light.png +0 -0
- data/app/assets/javascripts/bizside.js +1 -0
- data/app/assets/javascripts/jquery-treeTable/jquery.treeTable.js +400 -0
- data/app/assets/stylesheets/bizside.css +3 -0
- data/app/assets/stylesheets/jquery-treeTable/jquery.treeTable.css.erb +55 -0
- data/lib/bizside/acl/access_control_utils.rb +36 -0
- data/lib/bizside/acl/available_helper.rb +65 -0
- data/lib/bizside/acl/controller_helper.rb +15 -0
- data/lib/bizside/acl.rb +6 -0
- data/lib/bizside/active_record_logger.rb +3 -0
- data/lib/bizside/audit/job_logger.rb +14 -0
- data/lib/bizside/audit/logger.rb +35 -0
- data/lib/bizside/audit_log.rb +158 -0
- data/lib/bizside/cache/entry.rb +112 -0
- data/lib/bizside/cache/file_store.rb +167 -0
- data/lib/bizside/cache/store.rb +237 -0
- data/lib/bizside/cache_util.rb +47 -0
- data/lib/bizside/cache_utils.rb +10 -0
- data/lib/bizside/carrierwave.rb +48 -0
- data/lib/bizside/config.rb +63 -0
- data/lib/bizside/configurations/mail.rb +37 -0
- data/lib/bizside/configurations/prefix.rb +25 -0
- data/lib/bizside/configurations/storage.rb +28 -0
- data/lib/bizside/coverage/launch.rb +25 -0
- data/lib/bizside/coverage/rcov_formatter.rb +13 -0
- data/lib/bizside/cron_validator.rb +62 -0
- data/lib/bizside/cucumber.rb +1 -0
- data/lib/bizside/engine.rb +4 -0
- data/lib/bizside/file_converter.rb +56 -0
- data/lib/bizside/file_uploader.rb +71 -0
- data/lib/bizside/gengou.rb +46 -0
- data/lib/bizside/gengou.yml +5 -0
- data/lib/bizside/hanaita_conf.rb +88 -0
- data/lib/bizside/implicit_ftps.rb +29 -0
- data/lib/bizside/itamae_conf.rb +186 -0
- data/lib/bizside/job_utils.rb +285 -0
- data/lib/bizside/log_analyzer.rb +122 -0
- data/lib/bizside/mailer.rb +56 -0
- data/lib/bizside/query_builder.rb +78 -0
- data/lib/bizside/railtie.rb +80 -0
- data/lib/bizside/record_has_warnings.rb +4 -0
- data/lib/bizside/resque.rb +141 -0
- data/lib/bizside/rsync.rb +40 -0
- data/lib/bizside/safe_pty.rb +17 -0
- data/lib/bizside/shib_utils.rb +18 -0
- data/lib/bizside/show_exceptions.rb +18 -0
- data/lib/bizside/sql_utils.rb +45 -0
- data/lib/bizside/string_io.rb +39 -0
- data/lib/bizside/string_utils.rb +157 -0
- data/lib/bizside/task_helper.rb +263 -0
- data/lib/bizside/tasks.rb +6 -0
- data/lib/bizside/test_help.rb +17 -0
- data/lib/bizside/uploader/content_type_validator.rb +39 -0
- data/lib/bizside/uploader/default_extensions.yml +15 -0
- data/lib/bizside/uploader/exif.rb +43 -0
- data/lib/bizside/uploader/extension_whitelist.rb +26 -0
- data/lib/bizside/uploader/filename_validator.rb +31 -0
- data/lib/bizside/user_agent/action_view/action_view_4.rb +56 -0
- data/lib/bizside/user_agent/action_view/use_variant.rb +4 -0
- data/lib/bizside/user_agent/action_view.rb +10 -0
- data/lib/bizside/user_agent/controller_helper.rb +51 -0
- data/lib/bizside/user_agent.rb +108 -0
- data/lib/bizside/validations.rb +8 -0
- data/lib/bizside/version.rb +3 -0
- data/lib/bizside/view_helper.rb +10 -0
- data/lib/bizside/warning.rb +24 -0
- data/lib/bizside/yes.rb +16 -0
- data/lib/bizside.rb +96 -0
- data/lib/cron_validator.rb +3 -0
- data/lib/gengou.rb +3 -0
- data/lib/job_utils.rb +3 -0
- data/lib/query_builder.rb +3 -0
- data/lib/record_has_warnings.rb +3 -0
- data/lib/sql_utils.rb +3 -0
- data/lib/string_utils.rb +3 -0
- data/lib/user_agent.rb +3 -0
- data/lib/yes.rb +3 -0
- data/rails/locales/ja.yml +12 -0
- data/validations/collection_presence_validator.rb +15 -0
- data/validations/email_validator.rb +1 -0
- data/validations/ip_address_validator.rb +22 -0
- data/validations/tel_validator.rb +28 -0
- data/validations/url_validator.rb +30 -0
- data/validations/zip_validator.rb +35 -0
- metadata +467 -0
@@ -0,0 +1,263 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'erb'
|
3
|
+
require 'yaml'
|
4
|
+
require 'socket'
|
5
|
+
require 'bizside'
|
6
|
+
|
7
|
+
def self.rails_root
|
8
|
+
if ENV['RAILS_ROOT'].to_s.empty?
|
9
|
+
if interactive?
|
10
|
+
case ENV['RAILS_ENV']
|
11
|
+
when 'production'
|
12
|
+
ret = "/home/#{ENV['USER']}/rails_apps/#{self.add_on_name}/current"
|
13
|
+
else
|
14
|
+
if defined?(Rails)
|
15
|
+
ret = Rails.root.to_s
|
16
|
+
else
|
17
|
+
ret = `pwd`.strip
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
print "RAILS_ROOT [#{ret}]: "
|
22
|
+
answer = STDIN.gets.strip
|
23
|
+
ret = answer unless answer.empty?
|
24
|
+
else
|
25
|
+
case ENV['RAILS_ENV']
|
26
|
+
when 'test'
|
27
|
+
if defined?(Rails)
|
28
|
+
ret = Rails.root.to_s
|
29
|
+
else
|
30
|
+
ret = `pwd`.strip
|
31
|
+
end
|
32
|
+
end
|
33
|
+
puts "RAILS_ROOT [#{ret}]: "
|
34
|
+
end
|
35
|
+
else
|
36
|
+
ret = ENV['RAILS_ROOT']
|
37
|
+
puts "RAILS_ROOT [#{ret}]: "
|
38
|
+
end
|
39
|
+
|
40
|
+
ENV['RAILS_ROOT'] ||= ret
|
41
|
+
|
42
|
+
if ENV['RAILS_ROOT'].to_s.empty?
|
43
|
+
raise "必須です。処理を中止します。"
|
44
|
+
end
|
45
|
+
|
46
|
+
ret
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.rails_env
|
50
|
+
ret = 'development'
|
51
|
+
|
52
|
+
# RAILS_ENV が未指定の時は、BIZSIDE_ENV を利用できるか確認する
|
53
|
+
if ENV['RAILS_ENV'].to_s.empty?
|
54
|
+
unless ENV['BIZSIDE_ENV'].to_s.empty?
|
55
|
+
case ENV['BIZSIDE_ENV']
|
56
|
+
when 'production', 'staging'
|
57
|
+
ret = ENV['RAILS_ENV'] = 'production'
|
58
|
+
else
|
59
|
+
ret = ENV['RAILS_ENV'] = ENV['BIZSIDE_ENV']
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
if ENV['RAILS_ENV'].to_s.empty?
|
65
|
+
puts 'RAILS_ENVを選択してください。'
|
66
|
+
puts ' 1 - production'
|
67
|
+
puts '* 2 - development'
|
68
|
+
puts ' 3 - test'
|
69
|
+
puts
|
70
|
+
print '> '
|
71
|
+
|
72
|
+
if interactive?
|
73
|
+
selected = STDIN.gets.strip.to_i
|
74
|
+
ret = ['production', 'development', 'test'][selected - 1] if selected > 0
|
75
|
+
else
|
76
|
+
puts
|
77
|
+
raise "必須です。処理を中止します。"
|
78
|
+
end
|
79
|
+
else
|
80
|
+
ret = ENV['RAILS_ENV']
|
81
|
+
puts "RAILS_ENV: #{ret}"
|
82
|
+
end
|
83
|
+
|
84
|
+
ENV['RAILS_ENV'] ||= ret
|
85
|
+
ret
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.add_on_name
|
89
|
+
ret = ENV['ADD_ON_NAME']
|
90
|
+
unless ret
|
91
|
+
ret = Bizside.config.add_on_name if Bizside.config.add_on_name?
|
92
|
+
end
|
93
|
+
ret
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.prefix
|
97
|
+
ret = ENV['PREFIX']
|
98
|
+
unless ret
|
99
|
+
ret = Bizside.config.prefix if Bizside.config.prefix?
|
100
|
+
end
|
101
|
+
raise 'prefix は / から指定してください。' if ret and not ret.start_with?('/')
|
102
|
+
ret ||= '/' + add_on_name if Bizside.config.add_on_name?
|
103
|
+
ret ||= ask('プリフィックス', :required => true)
|
104
|
+
ret
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.interactive?
|
108
|
+
yes_confirmed?(ENV['INTERACTIVE'] || 'true')
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.gem_dir
|
112
|
+
File.expand_path('../../../..', __FILE__)
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.ask(prompt, options = {})
|
116
|
+
raise 'プロンプトは必須です。' if prompt.to_s.empty?
|
117
|
+
|
118
|
+
if options[:default].to_s.empty?
|
119
|
+
print prompt + ': '
|
120
|
+
elsif options[:password]
|
121
|
+
print prompt + " [FILTERED]: "
|
122
|
+
else
|
123
|
+
print prompt + " [#{options[:default]}]: "
|
124
|
+
end
|
125
|
+
|
126
|
+
answer = ''
|
127
|
+
if answer.empty? and options.has_key?(:env_key)
|
128
|
+
answer = ENV[options[:env_key].to_s].to_s
|
129
|
+
end
|
130
|
+
|
131
|
+
if answer.empty?
|
132
|
+
if interactive?
|
133
|
+
if options[:password]
|
134
|
+
system("stty -echo")
|
135
|
+
at_exit do
|
136
|
+
system("stty echo")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
answer = STDIN.gets.strip
|
141
|
+
|
142
|
+
if options[:password]
|
143
|
+
system("stty echo")
|
144
|
+
puts
|
145
|
+
end
|
146
|
+
else
|
147
|
+
puts
|
148
|
+
end
|
149
|
+
else
|
150
|
+
if options[:password]
|
151
|
+
puts 'FILTERED'
|
152
|
+
else
|
153
|
+
puts answer
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
if answer.empty? and options.has_key?(:default)
|
158
|
+
answer = options[:default].to_s
|
159
|
+
end
|
160
|
+
|
161
|
+
if answer.empty? and options[:required]
|
162
|
+
raise "必須です。処理を中止します。"
|
163
|
+
end
|
164
|
+
|
165
|
+
if block_given?
|
166
|
+
answer = yield answer
|
167
|
+
end
|
168
|
+
|
169
|
+
answer
|
170
|
+
end
|
171
|
+
|
172
|
+
def self.ask_yes(prompt, options = {})
|
173
|
+
answer = ask(prompt, options)
|
174
|
+
yes_confirmed?(answer, :fail_on_error => true)
|
175
|
+
end
|
176
|
+
|
177
|
+
def self.ask_env(env_key, options = {})
|
178
|
+
cache_file = 'tmp/cache/env'
|
179
|
+
cache = File.exists?(cache_file) ? YAML.load_file(cache_file) : {}
|
180
|
+
|
181
|
+
if options.fetch(:cache, false)
|
182
|
+
options = options.merge(default: cache.fetch(env_key, options[:default]))
|
183
|
+
end
|
184
|
+
|
185
|
+
ENV[env_key] ||= ask(env_key, options.merge(env_key: env_key))
|
186
|
+
|
187
|
+
if options.fetch(:cache, false)
|
188
|
+
FileUtils.mkdir_p(File.dirname(cache_file))
|
189
|
+
|
190
|
+
cache[env_key] = ENV[env_key]
|
191
|
+
File.write(cache_file, YAML.dump(cache))
|
192
|
+
else
|
193
|
+
if File.exists?(cache_file) and cache[env_key]
|
194
|
+
cache.delete(env_key)
|
195
|
+
File.write(cache_file, YAML.dump(cache))
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
ENV[env_key]
|
200
|
+
end
|
201
|
+
|
202
|
+
def self.yes_confirmed?(yes_value, options = {})
|
203
|
+
ret = Bizside::Yes.confirmed?(yes_value)
|
204
|
+
if ret.nil?
|
205
|
+
if options[:fail_on_error]
|
206
|
+
fail "yes/no または true/false 形式で入力してください。answer=#{answer}"
|
207
|
+
else
|
208
|
+
ret = false
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
ret
|
213
|
+
end
|
214
|
+
|
215
|
+
def self.ask_host(prompt, options = {})
|
216
|
+
if options.has_key?(:default)
|
217
|
+
ask(prompt, options)
|
218
|
+
else
|
219
|
+
ask(prompt, options.merge(default: dev_default_host))
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def self.dev_default_host
|
224
|
+
ret = nil
|
225
|
+
|
226
|
+
udp = UDPSocket.new
|
227
|
+
begin
|
228
|
+
# クラスBの先頭アドレス,echoポート 実際にはパケットは送信されない。
|
229
|
+
udp.connect("128.0.0.0", 7)
|
230
|
+
ret = Socket.unpack_sockaddr_in(udp.getsockname)[1]
|
231
|
+
ensure
|
232
|
+
udp.close
|
233
|
+
end
|
234
|
+
|
235
|
+
ret
|
236
|
+
end
|
237
|
+
|
238
|
+
def self.run(*commands)
|
239
|
+
commands.each do |c|
|
240
|
+
puts c
|
241
|
+
fail unless system(c)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def self.render(template, options = {})
|
246
|
+
FileUtils.mkdir_p(File.dirname(options[:to]))
|
247
|
+
File.write(options[:to], ERB.new(File.read(template), 0, '-').result)
|
248
|
+
end
|
249
|
+
|
250
|
+
def self.ip_addresses
|
251
|
+
unless @_ip_addresses
|
252
|
+
@_ip_addresses = []
|
253
|
+
`/sbin/ip route`.split("\n").each do |line|
|
254
|
+
elements = line.split.map(&:strip)
|
255
|
+
if index = elements.index('src')
|
256
|
+
@_ip_addresses << elements[index + 1]
|
257
|
+
end
|
258
|
+
end
|
259
|
+
@_ip_addresses
|
260
|
+
end
|
261
|
+
|
262
|
+
@_ip_addresses
|
263
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'minitest/reporters'
|
2
|
+
|
3
|
+
require_relative 'coverage/launch'
|
4
|
+
|
5
|
+
case ENV['CI'].to_s.downcase
|
6
|
+
when 'jenkins'
|
7
|
+
MiniTest::Reporters.use! [
|
8
|
+
MiniTest::Reporters::DefaultReporter.new,
|
9
|
+
MiniTest::Reporters::JUnitReporter.new
|
10
|
+
]
|
11
|
+
else
|
12
|
+
unless ENV['RM_INFO']
|
13
|
+
MiniTest::Reporters.use! [
|
14
|
+
MiniTest::Reporters::DefaultReporter.new(color: true)
|
15
|
+
]
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'mimemagic'
|
2
|
+
|
3
|
+
module Bizside
|
4
|
+
module Uploader
|
5
|
+
module ContentTypeValidator
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
begin
|
10
|
+
require 'carrierwave-magic'
|
11
|
+
include CarrierWave::Magic
|
12
|
+
process :set_magic_content_type => true
|
13
|
+
rescue => e
|
14
|
+
raise '[Bizside.gem ERROR] you need to add carrierwave-magic.gem.'
|
15
|
+
end
|
16
|
+
before :cache, :validate_content_type!
|
17
|
+
end
|
18
|
+
|
19
|
+
def content_type_checklist
|
20
|
+
%w(jpg jpeg gif png)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def validate_content_type!(new_file)
|
26
|
+
return if new_file.path.nil?
|
27
|
+
extension = new_file.extension.to_s
|
28
|
+
|
29
|
+
if content_type_checklist.include?(extension.downcase)
|
30
|
+
by_path = MimeMagic.by_extension(extension).to_s
|
31
|
+
unless new_file.content_type == by_path
|
32
|
+
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.content_type_whitelist_error", content_type: new_file.content_type)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'mimemagic'
|
2
|
+
|
3
|
+
module Bizside
|
4
|
+
module Uploader
|
5
|
+
module Exif
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
process :auto_orient
|
10
|
+
process :strip
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def auto_orient
|
16
|
+
return unless self.model.valid?
|
17
|
+
|
18
|
+
mime = MimeMagic.by_path(self.file.path)
|
19
|
+
return unless mime and mime.image?
|
20
|
+
|
21
|
+
manipulate! do |img|
|
22
|
+
img.auto_orient
|
23
|
+
img = yield(img) if block_given?
|
24
|
+
img
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def strip
|
29
|
+
return unless self.model.valid?
|
30
|
+
|
31
|
+
mime = MimeMagic.by_path(self.file.path)
|
32
|
+
return unless mime and mime.image?
|
33
|
+
|
34
|
+
manipulate! do |img|
|
35
|
+
img.strip
|
36
|
+
img = yield(img) if block_given?
|
37
|
+
img
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Bizside
|
2
|
+
module Uploader
|
3
|
+
module ExtensionWhitelist
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
default_extensions = Bizside.config.file_uploader.extensions_file_path.present? ? Bizside.config.file_uploader.extensions_file_path :
|
8
|
+
File.join(File.dirname(__FILE__), 'default_extensions.yml')
|
9
|
+
@@extensions = YAML.load_file(default_extensions).values
|
10
|
+
end
|
11
|
+
|
12
|
+
def extension_allowlist
|
13
|
+
return nil unless Bizside.config.file_uploader.extension_whitelist_enabled?
|
14
|
+
|
15
|
+
@@extensions
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def rails_logger_available?
|
21
|
+
defined?(Rails) && !Rails.logger.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Bizside
|
2
|
+
module Uploader
|
3
|
+
module FilenameValidator
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
before :cache, :validate_filename!
|
8
|
+
end
|
9
|
+
|
10
|
+
def invalid_filename_regexp
|
11
|
+
CARRIERWAVE_SANITIZE_REGEXP
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def validate_filename!(new_file)
|
17
|
+
if new_file and new_file.respond_to?(:original_filename)
|
18
|
+
filename = new_file.original_filename
|
19
|
+
else
|
20
|
+
filename = File.basename(new_file.path)
|
21
|
+
end
|
22
|
+
|
23
|
+
if invalid_filename_regexp =~ filename
|
24
|
+
message = I18n.translate(:'errors.messages.filename_error', filename: filename)
|
25
|
+
raise CarrierWave::IntegrityError, message
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
class ActionView::TemplateRenderer
|
4
|
+
|
5
|
+
def render(context, options)
|
6
|
+
@view = context
|
7
|
+
@details = extract_details(options)
|
8
|
+
template = get_template_by_user_agent(context, options)
|
9
|
+
context = @lookup_context
|
10
|
+
|
11
|
+
prepend_formats(template.formats)
|
12
|
+
|
13
|
+
unless context.rendered_format
|
14
|
+
context.rendered_format = template.formats.first || formats.first
|
15
|
+
end
|
16
|
+
|
17
|
+
render_template(template, options[:layout], options[:locals])
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def get_user_agent(context)
|
23
|
+
if context.respond_to?(:user_agent)
|
24
|
+
if context.respond_to?(:controller)
|
25
|
+
if context.controller.respond_to?(:session)
|
26
|
+
context.user_agent
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_template_by_user_agent(context, options)
|
33
|
+
ret = nil
|
34
|
+
option_for_template = options[:template]
|
35
|
+
|
36
|
+
ua = get_user_agent(context)
|
37
|
+
if ua
|
38
|
+
ua.priorities.each do |priority|
|
39
|
+
begin
|
40
|
+
options[:template] = option_for_template + '.' + priority
|
41
|
+
ret = determine_template(options)
|
42
|
+
break
|
43
|
+
rescue ActionView::MissingTemplate => e
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
unless ret
|
49
|
+
options[:template] = option_for_template
|
50
|
+
ret = determine_template(options)
|
51
|
+
end
|
52
|
+
|
53
|
+
Rails.logger.debug "UserAgent: #{ua ? ua.name : 'unknown'} => #{ret.identifier}"
|
54
|
+
ret
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
case Rails::VERSION::MAJOR
|
2
|
+
when 5
|
3
|
+
if Bizside.config.user_agent.use_variant?
|
4
|
+
load File.expand_path(File.join('action_view', 'use_variant.rb'), __dir__)
|
5
|
+
else
|
6
|
+
load File.expand_path(File.join('action_view', 'action_view_4.rb'), __dir__)
|
7
|
+
end
|
8
|
+
else
|
9
|
+
raise "Rails#{Rails::VERSION::MAJOR} はサポートしていません。"
|
10
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Bizside
|
2
|
+
class UserAgent
|
3
|
+
module ControllerHelper
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
case Rails::VERSION::MAJOR
|
8
|
+
when 3
|
9
|
+
before_filter :detect_user_agent
|
10
|
+
when 4
|
11
|
+
before_action :detect_user_agent
|
12
|
+
when 5
|
13
|
+
before_action :detect_user_agent
|
14
|
+
else
|
15
|
+
raise "Rails-#{Rails::VERSION::MAJOR} は未対応です。"
|
16
|
+
end
|
17
|
+
|
18
|
+
::ActionController::Base.helper_method :user_agent
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def detect_user_agent
|
24
|
+
if params[:ua].present?
|
25
|
+
self.user_agent = ::Bizside::UserAgent.new(params[:ua], request.env['HTTP_USER_AGENT'])
|
26
|
+
else
|
27
|
+
self.user_agent = ::Bizside::UserAgent.parse(request.env['HTTP_USER_AGENT'])
|
28
|
+
end
|
29
|
+
|
30
|
+
set_request_variant if Bizside.config.user_agent.use_variant?
|
31
|
+
|
32
|
+
request.env['BIZSIDE_DEVICE'] = self.user_agent.name
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_request_variant
|
36
|
+
if self.user_agent.present? && self.user_agent.priorities.present?
|
37
|
+
request.variant = self.user_agent.priorities.map(&:to_sym)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def user_agent
|
42
|
+
@_user_agent
|
43
|
+
end
|
44
|
+
|
45
|
+
def user_agent=(value)
|
46
|
+
@_user_agent = value
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module Bizside
|
2
|
+
class UserAgent
|
3
|
+
|
4
|
+
USER_AGENTS = [
|
5
|
+
ANDROID_MOBILE = 'android',
|
6
|
+
IPAD = 'ipad',
|
7
|
+
IPHONE = 'iphone',
|
8
|
+
PC = 'pc',
|
9
|
+
SMART_PHONE = 'sp',
|
10
|
+
]
|
11
|
+
|
12
|
+
def self.parse(http_user_agent)
|
13
|
+
case http_user_agent
|
14
|
+
when /Android.*Mobile/
|
15
|
+
new(ANDROID_MOBILE, http_user_agent)
|
16
|
+
when /iPhone/
|
17
|
+
new(IPHONE, http_user_agent)
|
18
|
+
when /iPad/
|
19
|
+
new(IPAD, http_user_agent)
|
20
|
+
else
|
21
|
+
new(PC, http_user_agent)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(name, http_user_agent = nil)
|
26
|
+
@name = name if USER_AGENTS.include?(name)
|
27
|
+
@name ||= PC
|
28
|
+
@http_user_agent = http_user_agent
|
29
|
+
end
|
30
|
+
|
31
|
+
def name
|
32
|
+
@name
|
33
|
+
end
|
34
|
+
|
35
|
+
def actual
|
36
|
+
@actual ||= @http_user_agent ? self.class.parse(@http_user_agent) : self
|
37
|
+
end
|
38
|
+
|
39
|
+
def ipad?
|
40
|
+
self.name == IPAD
|
41
|
+
end
|
42
|
+
|
43
|
+
def pc?
|
44
|
+
self.name == PC
|
45
|
+
end
|
46
|
+
|
47
|
+
def windows?
|
48
|
+
case @http_user_agent
|
49
|
+
when /.*Windows.*/
|
50
|
+
pc?
|
51
|
+
else
|
52
|
+
false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def mac?
|
57
|
+
case @http_user_agent
|
58
|
+
when /.*Mac.*/
|
59
|
+
pc?
|
60
|
+
else
|
61
|
+
false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def sp?
|
66
|
+
[ANDROID_MOBILE, IPHONE, SMART_PHONE].include?(self.name)
|
67
|
+
end
|
68
|
+
|
69
|
+
def iphone?
|
70
|
+
self.name == IPHONE
|
71
|
+
end
|
72
|
+
|
73
|
+
def android_mobile?
|
74
|
+
self.name == ANDROID_MOBILE
|
75
|
+
end
|
76
|
+
|
77
|
+
def priorities
|
78
|
+
ret = []
|
79
|
+
ret << self.name
|
80
|
+
ret << SMART_PHONE if sp?
|
81
|
+
ret << PC unless pc?
|
82
|
+
ret
|
83
|
+
end
|
84
|
+
|
85
|
+
def ie?
|
86
|
+
case @http_user_agent
|
87
|
+
when /.* MSIE .* Windows .*/ #IE10以下
|
88
|
+
true
|
89
|
+
when /.*Windows.*Trident.*/ #IE11
|
90
|
+
true
|
91
|
+
else
|
92
|
+
false
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def chrome?
|
97
|
+
case @http_user_agent
|
98
|
+
when /.* Chrome\/.*/
|
99
|
+
true
|
100
|
+
else
|
101
|
+
false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
require_relative 'user_agent/controller_helper'
|