chanko 2.0.0 → 2.3.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 +7 -0
- data/.github/workflows/master_and_pr.yml +64 -0
- data/CHANGELOG.md +42 -0
- data/MIT-LICENSE +1 -1
- data/README.md +30 -9
- data/Rakefile +2 -9
- data/bin/setup +8 -0
- data/bin/test +7 -0
- data/chanko.gemspec +8 -4
- data/gemfiles/Gemfile_rails_5.0.rb +6 -0
- data/gemfiles/Gemfile_rails_5.0.rb.lock +203 -0
- data/gemfiles/Gemfile_rails_5.1.rb +6 -0
- data/gemfiles/Gemfile_rails_5.1.rb.lock +203 -0
- data/gemfiles/Gemfile_rails_5.2.rb +6 -0
- data/gemfiles/Gemfile_rails_5.2.rb.lock +209 -0
- data/gemfiles/Gemfile_rails_6.0.rb +8 -0
- data/gemfiles/Gemfile_rails_6.0.rb.lock +238 -0
- data/gemfiles/Gemfile_rails_6.1.rb +8 -0
- data/gemfiles/Gemfile_rails_6.1.rb.lock +241 -0
- data/lib/chanko/active_if.rb +42 -10
- data/lib/chanko/config.rb +20 -2
- data/lib/chanko/controller.rb +1 -1
- data/lib/chanko/exception_handler.rb +32 -5
- data/lib/chanko/function.rb +3 -1
- data/lib/chanko/invoker.rb +25 -4
- data/lib/chanko/loader.rb +127 -43
- data/lib/chanko/railtie.rb +33 -6
- data/lib/chanko/resolver/no_cache_file_system_resolver.rb +13 -0
- data/lib/chanko/test.rb +6 -1
- data/lib/chanko/unit/scope_finder.rb +7 -7
- data/lib/chanko/unit.rb +5 -1
- data/lib/chanko/unit_proxy.rb +4 -0
- data/lib/chanko/version.rb +1 -1
- data/lib/generators/chanko/unit/templates/unit.rb.erb +7 -1
- data/spec/chanko/controller_spec.rb +5 -5
- data/spec/chanko/deprecated_spec.rb +27 -0
- data/spec/chanko/exception_handler_spec.rb +23 -2
- data/spec/chanko/function_spec.rb +34 -8
- data/spec/chanko/helper_spec.rb +1 -1
- data/spec/chanko/invoker_spec.rb +62 -36
- data/spec/chanko/loader_spec.rb +14 -12
- data/spec/chanko/logger_spec.rb +13 -7
- data/spec/chanko/test_spec.rb +17 -7
- data/spec/chanko/unit/extender_spec.rb +3 -3
- data/spec/chanko/unit/scope_finder_spec.rb +4 -4
- data/spec/chanko/unit_proxy_provider_spec.rb +12 -12
- data/spec/chanko/unit_proxy_spec.rb +3 -3
- data/spec/chanko/unit_spec.rb +107 -33
- data/spec/controllers/application_controller_spec.rb +2 -2
- data/spec/dummy/README.md +12 -0
- data/spec/dummy/app/assets/config/manifest.js +3 -0
- data/spec/dummy/app/controllers/entries_controller.rb +2 -2
- data/spec/dummy/app/models/entry.rb +0 -1
- data/spec/dummy/app/units/entry_deletion/entry_deletion.rb +6 -0
- data/spec/dummy/config/application.rb +15 -17
- data/spec/dummy/config/boot.rb +4 -5
- data/spec/dummy/config/environment.rb +1 -1
- data/spec/dummy/config/environments/development.rb +2 -16
- data/spec/dummy/config/environments/production.rb +5 -1
- data/spec/dummy/config/environments/test.rb +10 -7
- data/spec/dummy/config/initializers/chanko_initializer.rb +0 -0
- data/spec/dummy/config/initializers/secret_token.rb +1 -0
- data/spec/dummy/config/storage.yml +1 -0
- data/spec/fixtures/units/example_unit/example_unit.rb +17 -0
- data/spec/fixtures/units/sensitive_inactive_unit/sensitive_inactive_unit.rb +9 -0
- data/spec/fixtures/units/sensitive_unit/sensitive_unit.rb +16 -0
- data/spec/spec_helper.rb +35 -5
- metadata +112 -70
- data/.travis.yml +0 -5
- data/Gemfile +0 -26
- data/spec/dummy/README.rdoc +0 -261
data/lib/chanko/active_if.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
module Chanko
|
|
2
2
|
class ActiveIf
|
|
3
|
+
class NoConditionFound < StandardError
|
|
4
|
+
end
|
|
5
|
+
|
|
3
6
|
class << self
|
|
4
7
|
def define(label, &block)
|
|
5
8
|
definitions[label] = block
|
|
@@ -18,10 +21,18 @@ module Chanko
|
|
|
18
21
|
end
|
|
19
22
|
end
|
|
20
23
|
|
|
21
|
-
attr_reader :conditions
|
|
24
|
+
attr_reader :conditions
|
|
25
|
+
|
|
26
|
+
def options # remains just for backward compatibility
|
|
27
|
+
warn "ActiveIf#options is deprecated and is never used at #{caller[0]}"
|
|
28
|
+
@options
|
|
29
|
+
end
|
|
22
30
|
|
|
23
31
|
def initialize(*conditions, &block)
|
|
24
32
|
@options = conditions.extract_options!
|
|
33
|
+
unless @options.empty?
|
|
34
|
+
warn "options in ActiveIf#new are deprecated and are never used at #{caller[0]}"
|
|
35
|
+
end
|
|
25
36
|
@conditions = conditions
|
|
26
37
|
@block = block
|
|
27
38
|
end
|
|
@@ -33,26 +44,47 @@ module Chanko
|
|
|
33
44
|
def blocks
|
|
34
45
|
@blocks ||= begin
|
|
35
46
|
conditions.map do |condition|
|
|
36
|
-
|
|
47
|
+
Block.new(condition)
|
|
37
48
|
end << @block
|
|
38
49
|
end.compact
|
|
39
50
|
end
|
|
40
51
|
|
|
41
|
-
class
|
|
42
|
-
def initialize(*
|
|
43
|
-
@
|
|
52
|
+
class Block
|
|
53
|
+
def initialize(*conditions)
|
|
54
|
+
@conditions = conditions
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def call(context, options)
|
|
58
|
+
block.call(context, options)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def block
|
|
62
|
+
condition = @conditions.first
|
|
63
|
+
if condition.kind_of?(Block)
|
|
64
|
+
condition
|
|
65
|
+
else
|
|
66
|
+
ActiveIf.find(condition) or raise NoConditionFound, condition
|
|
67
|
+
end
|
|
44
68
|
end
|
|
69
|
+
end
|
|
45
70
|
|
|
46
|
-
|
|
71
|
+
class Any < Block
|
|
72
|
+
def block
|
|
47
73
|
proc do |context, options|
|
|
48
|
-
|
|
49
|
-
|
|
74
|
+
@conditions.any? do |condition|
|
|
75
|
+
Block.new(condition).call(context, options)
|
|
50
76
|
end
|
|
51
77
|
end
|
|
52
78
|
end
|
|
79
|
+
end
|
|
53
80
|
|
|
54
|
-
|
|
55
|
-
|
|
81
|
+
class None < Block
|
|
82
|
+
def block
|
|
83
|
+
proc do |context, options|
|
|
84
|
+
@conditions.none? do |condition|
|
|
85
|
+
Block.new(condition).call(context, options)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
56
88
|
end
|
|
57
89
|
end
|
|
58
90
|
end
|
data/lib/chanko/config.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require 'chanko/resolver/no_cache_file_system_resolver'
|
|
2
|
+
|
|
1
3
|
module Chanko
|
|
2
4
|
module Config
|
|
3
5
|
class << self
|
|
@@ -7,10 +9,10 @@ module Chanko
|
|
|
7
9
|
:cache_units,
|
|
8
10
|
:compatible_css_class,
|
|
9
11
|
:enable_logger,
|
|
12
|
+
:propagated_errors,
|
|
10
13
|
:proxy_method_name,
|
|
11
14
|
:raise_error,
|
|
12
15
|
:resolver,
|
|
13
|
-
:units_directory_path
|
|
14
16
|
)
|
|
15
17
|
|
|
16
18
|
def reset
|
|
@@ -18,11 +20,27 @@ module Chanko
|
|
|
18
20
|
self.backtrace_limit = 10
|
|
19
21
|
self.compatible_css_class = false
|
|
20
22
|
self.enable_logger = true
|
|
23
|
+
self.propagated_errors = []
|
|
21
24
|
self.proxy_method_name = :unit
|
|
22
25
|
self.raise_error = Rails.env.development?
|
|
23
|
-
self.resolver
|
|
26
|
+
self.resolver = resolver_for_using_rails_and_env
|
|
24
27
|
self.units_directory_path = "app/units"
|
|
25
28
|
end
|
|
29
|
+
|
|
30
|
+
def units_directory_path=(path)
|
|
31
|
+
@units_directory_path = path
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def units_directory_path
|
|
35
|
+
@resolved_units_directory_path ||= Rails.root.join(@units_directory_path).to_s
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def resolver_for_using_rails_and_env
|
|
39
|
+
return ActionView::FileSystemResolver if Rails::VERSION::MAJOR >= 7
|
|
40
|
+
return Chanko::Resolver::NoCacheFileSystemResolver if Rails.env.development?
|
|
41
|
+
return ActionView::OptimizedFileSystemResolver
|
|
42
|
+
end
|
|
43
|
+
private :resolver_for_using_rails_and_env
|
|
26
44
|
end
|
|
27
45
|
|
|
28
46
|
reset
|
data/lib/chanko/controller.rb
CHANGED
|
@@ -1,10 +1,37 @@
|
|
|
1
1
|
module Chanko
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
class ExceptionHandler
|
|
3
|
+
def self.handle(*args)
|
|
4
|
+
new(*args).handle
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
attr_reader :exception, :unit
|
|
8
|
+
|
|
9
|
+
def initialize(exception, unit = nil)
|
|
10
|
+
@exception = exception
|
|
11
|
+
@unit = unit
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def handle
|
|
15
|
+
if propagated?
|
|
16
|
+
raise exception
|
|
17
|
+
else
|
|
18
|
+
log
|
|
19
|
+
raise exception if raised?
|
|
7
20
|
end
|
|
8
21
|
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def propagated?
|
|
26
|
+
Config.propagated_errors.any? {|klass| exception.is_a?(klass) }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def raised?
|
|
30
|
+
unit.try(:raise_error?) || Config.raise_error
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def log
|
|
34
|
+
Logger.debug(exception)
|
|
35
|
+
end
|
|
9
36
|
end
|
|
10
37
|
end
|
data/lib/chanko/function.rb
CHANGED
data/lib/chanko/invoker.rb
CHANGED
|
@@ -81,10 +81,17 @@ module Chanko
|
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
def __invoke_default_block
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
current_default_block = __default_block
|
|
85
|
+
begin
|
|
86
|
+
__without_default_stack do
|
|
87
|
+
__without_locals_stack do
|
|
88
|
+
if view?
|
|
89
|
+
capture(¤t_default_block)
|
|
90
|
+
else
|
|
91
|
+
instance_exec(¤t_default_block)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
88
95
|
end
|
|
89
96
|
end
|
|
90
97
|
|
|
@@ -101,5 +108,19 @@ module Chanko
|
|
|
101
108
|
ensure
|
|
102
109
|
__unit_locals_stack.pop
|
|
103
110
|
end
|
|
111
|
+
|
|
112
|
+
def __without_default_stack
|
|
113
|
+
default = __defaults_stack.pop
|
|
114
|
+
yield
|
|
115
|
+
ensure
|
|
116
|
+
__defaults_stack << default
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def __without_locals_stack
|
|
120
|
+
locals = __unit_locals_stack.pop
|
|
121
|
+
yield
|
|
122
|
+
ensure
|
|
123
|
+
__unit_locals_stack << locals
|
|
124
|
+
end
|
|
104
125
|
end
|
|
105
126
|
end
|
data/lib/chanko/loader.rb
CHANGED
|
@@ -1,65 +1,149 @@
|
|
|
1
1
|
require "pathname"
|
|
2
|
-
|
|
3
2
|
module Chanko
|
|
4
|
-
|
|
5
|
-
class
|
|
6
|
-
def load(unit_name)
|
|
7
|
-
new(unit_name).load
|
|
8
|
-
end
|
|
3
|
+
module Loader
|
|
4
|
+
class MissingEagarLoadSettingError < StandardError; end
|
|
9
5
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
end
|
|
6
|
+
class << self
|
|
7
|
+
delegate :load, :cache, :eager_load_units!, to: "loader"
|
|
13
8
|
end
|
|
14
9
|
|
|
15
|
-
def
|
|
16
|
-
|
|
10
|
+
def self.loader
|
|
11
|
+
zeitwerk? ? ZeitwerkLoader : ClassicLoader
|
|
17
12
|
end
|
|
18
13
|
|
|
19
|
-
def
|
|
20
|
-
|
|
21
|
-
load_from_cache
|
|
22
|
-
else
|
|
23
|
-
load_from_file
|
|
24
|
-
end
|
|
14
|
+
def self.zeitwerk?
|
|
15
|
+
Rails.respond_to?(:autoloaders) && Rails.autoloaders.zeitwerk_enabled?
|
|
25
16
|
end
|
|
26
17
|
|
|
27
|
-
def
|
|
28
|
-
|
|
18
|
+
def self.classic?
|
|
19
|
+
!zeitwerk?
|
|
29
20
|
end
|
|
30
21
|
|
|
31
|
-
def
|
|
32
|
-
|
|
22
|
+
def self.prepare_eager_load(mode: )
|
|
23
|
+
if mode == :zeitwerk && zeitwerk?
|
|
24
|
+
self.loader.prepare_eager_load
|
|
25
|
+
elsif mode == :classic && classic?
|
|
26
|
+
self.loader.prepare_eager_load
|
|
27
|
+
end
|
|
33
28
|
end
|
|
34
29
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
ExceptionHandler.handle(exception)
|
|
40
|
-
cache[@name] = false
|
|
41
|
-
nil
|
|
42
|
-
end
|
|
30
|
+
class ZeitwerkLoader
|
|
31
|
+
def self.load(name)
|
|
32
|
+
self.new(name).load
|
|
33
|
+
end
|
|
43
34
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
35
|
+
def self.cache
|
|
36
|
+
# backward compatibility
|
|
37
|
+
{ }
|
|
38
|
+
end
|
|
48
39
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
40
|
+
def self.eager_load_units!
|
|
41
|
+
# Zeitwerk load chanko units as default
|
|
42
|
+
end
|
|
52
43
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
def self.prepare_eager_load
|
|
45
|
+
add_unit_directory_to_eager_load_paths
|
|
46
|
+
Rails.autoloaders.main.collapse(Chanko::Config.units_directory_path + '/*')
|
|
47
|
+
Rails.autoloaders.main.ignore(Chanko::Config.units_directory_path + '/*/spec*')
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.add_unit_directory_to_eager_load_paths
|
|
51
|
+
path = Chanko::Config.units_directory_path
|
|
52
|
+
|
|
53
|
+
unless Rails.configuration.eager_load_paths.include?(path)
|
|
54
|
+
Rails.configuration.eager_load_paths << path
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def initialize(name)
|
|
59
|
+
@name = name
|
|
60
|
+
end
|
|
56
61
|
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
def load
|
|
63
|
+
constantize
|
|
64
|
+
rescue NameError
|
|
65
|
+
# Chanko never raise error even if the constant fails to reference
|
|
66
|
+
false
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def constantize
|
|
70
|
+
@name.to_s.camelize.constantize
|
|
71
|
+
end
|
|
59
72
|
end
|
|
60
73
|
|
|
61
|
-
|
|
62
|
-
self.
|
|
74
|
+
class ClassicLoader
|
|
75
|
+
def self.cache
|
|
76
|
+
@cache ||= {}
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.eager_load_units!
|
|
80
|
+
Pathname.glob("#{Chanko::Config.units_directory_path}/*").select(&:directory?).each do |path|
|
|
81
|
+
Chanko::Loader::ClassicLoader.load(path.basename.to_s.to_sym)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.prepare_eager_load
|
|
86
|
+
raise MissingEagarLoadSettingError if Rails.configuration.eager_load.nil?
|
|
87
|
+
|
|
88
|
+
if Rails.configuration.eager_load
|
|
89
|
+
ruleout_unit_files_from_rails_eager_loading
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.ruleout_unit_files_from_rails_eager_loading
|
|
94
|
+
Rails.configuration.eager_load_paths.delete(Chanko::Config.units_directory_path)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def self.load(name)
|
|
98
|
+
self.new(name).load
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def self.load_from_cache(name)
|
|
102
|
+
self.cache[name]
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.save_to_cache(name, unit)
|
|
106
|
+
self.cache[name] = unit
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def initialize(name)
|
|
110
|
+
@name = name
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def load
|
|
114
|
+
load_from_cache.then do |unit|
|
|
115
|
+
next unit unless unit.nil?
|
|
116
|
+
load_from_file_and_store_to_cache
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def load_from_file_and_store_to_cache
|
|
121
|
+
add_autoload_path
|
|
122
|
+
constantize.tap do |unit|
|
|
123
|
+
self.class.save_to_cache(@name, unit)
|
|
124
|
+
end
|
|
125
|
+
rescue Exception => exception
|
|
126
|
+
ExceptionHandler.handle(exception)
|
|
127
|
+
self.class.save_to_cache(@name, false)
|
|
128
|
+
false
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def load_from_cache
|
|
132
|
+
self.class.load_from_cache(@name)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def add_autoload_path
|
|
136
|
+
ActiveSupport::Dependencies.autoload_paths << autoload_path
|
|
137
|
+
ActiveSupport::Dependencies.autoload_paths.uniq!
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def autoload_path
|
|
141
|
+
"#{Config.units_directory_path }/#{@name}"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def constantize
|
|
145
|
+
@name.to_s.camelize.constantize
|
|
146
|
+
end
|
|
63
147
|
end
|
|
64
148
|
end
|
|
65
149
|
end
|
data/lib/chanko/railtie.rb
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
1
|
module Chanko
|
|
2
2
|
class Railtie < Rails::Railtie
|
|
3
|
-
initializer "chanko" do |app|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
initializer "chanko.include", before: :eager_load! do |app|
|
|
4
|
+
ActiveSupport.on_load :action_view do
|
|
5
|
+
::ActionView::Base.send(:include, Helper, Invoker, UnitProxyProvider)
|
|
6
|
+
end
|
|
7
|
+
ActiveSupport.on_load :action_controller do
|
|
8
|
+
::ActionController::Base.send(:include, Controller, Invoker, UnitProxyProvider)
|
|
9
|
+
end
|
|
10
|
+
ActiveSupport.on_load :active_record do
|
|
11
|
+
::ActiveRecord::Base.send(:include, UnitProxyProvider)
|
|
12
|
+
::ActiveRecord::Relation.send(:include, UnitProxyProvider)
|
|
13
|
+
::ActiveRecord::Associations::CollectionAssociation.send(:include, UnitProxyProvider)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
initializer("chanko.zeitwerk.prepare_eager_load", before: :set_autoload_paths) do |app|
|
|
19
|
+
# zeitwerk freezes autoload_paths after :set_autoload_paths.
|
|
20
|
+
# So we need to prepare before set_autoload_paths
|
|
21
|
+
Chanko::Loader.prepare_eager_load(mode: :zeitwerk)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
initializer("chanko.classic.prepare_eager_load", after: :load_environment_config) do |app|
|
|
25
|
+
# Rails5 doens't load environments/*.rb files before :set_autoload_paths.
|
|
26
|
+
# In other words, at this stage, config.eager_load cannot be determined to be true or false.
|
|
27
|
+
# But classic loader does not freeze paths on :set_autoload_paths.
|
|
28
|
+
# After all, It's ok if it is executed after :set_autoload_paths on Rails5 and Rails6(classic).
|
|
29
|
+
Chanko::Loader.prepare_eager_load(mode: :classic)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
initializer("chanko.eager_load_units", before: :eager_load!) do |app|
|
|
33
|
+
# This is why we need handmade eager-loading
|
|
34
|
+
# https://github.com/cookpad/chanko/pull/38
|
|
35
|
+
Chanko::Loader.eager_load_units!
|
|
9
36
|
end
|
|
10
37
|
end
|
|
11
38
|
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
unless defined?(ActionView::FileSystemResolver)
|
|
2
|
+
require 'action_view/template/resolver'
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
module Chanko
|
|
6
|
+
module Resolver
|
|
7
|
+
class NoCacheFileSystemResolver < ActionView::FileSystemResolver
|
|
8
|
+
def query(path, details, formats, locals, cache:)
|
|
9
|
+
super(path, details, formats, locals, cache: false)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/lib/chanko/test.rb
CHANGED
|
@@ -8,6 +8,10 @@ module Chanko
|
|
|
8
8
|
def included(base)
|
|
9
9
|
base.send :include, UnitProxyProvider
|
|
10
10
|
end
|
|
11
|
+
|
|
12
|
+
def logger
|
|
13
|
+
@logger = ::Logger.new(STDOUT)
|
|
14
|
+
end
|
|
11
15
|
end
|
|
12
16
|
|
|
13
17
|
def enable_unit(unit_name)
|
|
@@ -33,7 +37,8 @@ module Chanko
|
|
|
33
37
|
active_without_activations?(*args)
|
|
34
38
|
end
|
|
35
39
|
end
|
|
36
|
-
|
|
40
|
+
alias_method :active_without_activations?, :active?
|
|
41
|
+
alias_method :active?, :active_with_activations?
|
|
37
42
|
end
|
|
38
43
|
end
|
|
39
44
|
end
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
module Chanko
|
|
2
2
|
module Unit
|
|
3
3
|
class ScopeFinder
|
|
4
|
-
LABEL_SCOPE_MAP = {
|
|
5
|
-
:controller => ActionController::Base,
|
|
6
|
-
:model => ActiveRecord::Base,
|
|
7
|
-
:view => ActionView::Base,
|
|
8
|
-
}
|
|
9
|
-
|
|
10
4
|
def self.find(*args)
|
|
11
5
|
new(*args).find
|
|
12
6
|
end
|
|
@@ -34,7 +28,13 @@ module Chanko
|
|
|
34
28
|
end
|
|
35
29
|
|
|
36
30
|
def label
|
|
37
|
-
|
|
31
|
+
label_scope_map = {
|
|
32
|
+
:controller => ActionController::Base,
|
|
33
|
+
:model => ActiveRecord::Base,
|
|
34
|
+
:view => ActionView::Base
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
label_scope_map[@identifier]
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
end
|
data/lib/chanko/unit.rb
CHANGED
|
@@ -49,6 +49,10 @@ module Chanko
|
|
|
49
49
|
ActiveIf::Any.new(*labels)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
+
def none(*labels)
|
|
53
|
+
ActiveIf::None.new(*labels)
|
|
54
|
+
end
|
|
55
|
+
|
|
52
56
|
def raise_error
|
|
53
57
|
@raise_error = true
|
|
54
58
|
end
|
|
@@ -67,7 +71,7 @@ module Chanko
|
|
|
67
71
|
end
|
|
68
72
|
|
|
69
73
|
def view_path
|
|
70
|
-
"#{Config.units_directory_path}/#{unit_name}/views"
|
|
74
|
+
Rails.root.join("#{Config.units_directory_path}/#{unit_name}/views").to_s
|
|
71
75
|
end
|
|
72
76
|
|
|
73
77
|
def find_function(identifier, label)
|
data/lib/chanko/unit_proxy.rb
CHANGED
|
@@ -26,5 +26,9 @@ module Chanko
|
|
|
26
26
|
def method_missing(method_name, *args, &block)
|
|
27
27
|
@context.send("#{prefix}#{method_name}", *args, &block)
|
|
28
28
|
end
|
|
29
|
+
|
|
30
|
+
def respond_to_missing?(method_name, include_private)
|
|
31
|
+
@context.respond_to?("#{prefix}#{method_name}")
|
|
32
|
+
end
|
|
29
33
|
end
|
|
30
34
|
end
|
data/lib/chanko/version.rb
CHANGED
|
@@ -48,7 +48,7 @@ module <%= class_name %>
|
|
|
48
48
|
# ## models
|
|
49
49
|
# In models block, you can expand model features by `expand` method.
|
|
50
50
|
# The expanded methods are available via unit proxy like `User.unit.active`,
|
|
51
|
-
#
|
|
51
|
+
# `User.find(params[:id]).unit.active?` or `User.unit.gc_all_soft_deleted_users`.
|
|
52
52
|
#
|
|
53
53
|
# ```
|
|
54
54
|
# models do
|
|
@@ -58,6 +58,12 @@ module <%= class_name %>
|
|
|
58
58
|
# def active?
|
|
59
59
|
# deleted_at.nil?
|
|
60
60
|
# end
|
|
61
|
+
#
|
|
62
|
+
# class_methods do
|
|
63
|
+
# def gc_all_soft_deleted_users
|
|
64
|
+
# where.not(deleted_at: nil).delete_all
|
|
65
|
+
# end
|
|
66
|
+
# end
|
|
61
67
|
# end
|
|
62
68
|
# end
|
|
63
69
|
# ```
|
|
@@ -22,21 +22,21 @@ module Chanko
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it "defines an action to invoke unit function" do
|
|
25
|
-
controller.test.
|
|
25
|
+
expect(controller.test).to eq("test")
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it "defines 2 actions at one line" do
|
|
29
|
-
controller.foo.
|
|
30
|
-
controller.bar.
|
|
29
|
+
expect(controller.foo).to eq("foo")
|
|
30
|
+
expect(controller.bar).to eq("bar")
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
it "is aliased with `ext_action`" do
|
|
34
|
-
controller.alias.
|
|
34
|
+
expect(controller.alias).to eq("alias")
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
context "when invoke is fallen back" do
|
|
38
38
|
it "halts with 400 status code" do
|
|
39
|
-
controller.error.
|
|
39
|
+
expect(controller.error).to eq("Bad Request 400")
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
module Chanko
|
|
4
|
+
describe ActiveIf do
|
|
5
|
+
around do |example|
|
|
6
|
+
origin, $stderr = $stderr, StringIO.new
|
|
7
|
+
example.run
|
|
8
|
+
$stderr = origin
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe ".new" do
|
|
12
|
+
context "with option arguments" do
|
|
13
|
+
it "prints warning message on standard error output" do
|
|
14
|
+
ActiveIf.new(foo: "bar")
|
|
15
|
+
expect($stderr.string).to match(/\Aoptions in ActiveIf#new are deprecated and are never used at #{__FILE__}/)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe "#options" do
|
|
21
|
+
it "prints warning message on standard error output" do
|
|
22
|
+
ActiveIf.new.options
|
|
23
|
+
expect($stderr.string).to match(/\AActiveIf#options is deprecated and is never used at #{__FILE__}/)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|