rails_console_toolkit 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -27
- data/lib/rails_console_toolkit/aliases.rb +1 -1
- data/lib/rails_console_toolkit/helpers.rb +56 -0
- data/lib/rails_console_toolkit/version.rb +1 -1
- data/lib/rails_console_toolkit.rb +8 -44
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cd8126fdabd86bec0f64ff953337ae9a4a1435114ea35018406f322b6d7f8c6
|
4
|
+
data.tar.gz: 3a0984cfe0561fc91ba564ca94d0d97e97e8246628a55da85e408e8e32f24699
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1261ef9b37861766bee33d0d06846042b6068aac61eec05bd9f4c606f42b98a46f8ef0ba6d6969c6a672724e91bed18c427a60263c30cf82155a0c354a457ae
|
7
|
+
data.tar.gz: 43668cca3112f56ddf0aa255f2d75c4d607c75d4a348a0ca49426a441f6c0c6123d9423bf8ca1e87b9bfc7435cfee137ab1b02c5c150e67290546b9d4854fb8d
|
data/README.md
CHANGED
@@ -14,14 +14,15 @@ Add this line to your application's Gemfile:
|
|
14
14
|
gem 'rails_console_toolkit'
|
15
15
|
```
|
16
16
|
|
17
|
-
And then
|
17
|
+
And then configure it within an initializer:
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
Or install it yourself as:
|
22
|
-
|
23
|
-
$ gem install rails_console_toolkit
|
19
|
+
```ruby
|
20
|
+
# config/initializers/console.rb
|
24
21
|
|
22
|
+
RailsConsoleToolkit.configure do |config|
|
23
|
+
# helper definitions go here...
|
24
|
+
end
|
25
|
+
```
|
25
26
|
|
26
27
|
## Helper packs
|
27
28
|
|
@@ -30,7 +31,9 @@ Or install it yourself as:
|
|
30
31
|
```ruby
|
31
32
|
# config/initializers/console.rb
|
32
33
|
|
33
|
-
|
34
|
+
RailsConsoleToolkit.configure do |config|
|
35
|
+
config.use_pack :aliases
|
36
|
+
end
|
34
37
|
```
|
35
38
|
|
36
39
|
```
|
@@ -44,7 +47,9 @@ require 'rails_console_toolkit/aliases'
|
|
44
47
|
```ruby
|
45
48
|
# config/initializers/console.rb
|
46
49
|
|
47
|
-
|
50
|
+
RailsConsoleToolkit.configure do |config|
|
51
|
+
config.use_pack :utils
|
52
|
+
end
|
48
53
|
```
|
49
54
|
|
50
55
|
```
|
@@ -61,7 +66,9 @@ foo (3000.6ms)
|
|
61
66
|
```ruby
|
62
67
|
# config/initializers/console.rb
|
63
68
|
|
64
|
-
|
69
|
+
RailsConsoleToolkit.configure do |config|
|
70
|
+
config.use_pack :solidus
|
71
|
+
end
|
65
72
|
```
|
66
73
|
|
67
74
|
```
|
@@ -74,24 +81,17 @@ require 'rails_console_toolkit/solidus'
|
|
74
81
|
> country(...) # => will look for Spree::Country records by :id, :iso, :iso3, :iso_name, :name
|
75
82
|
```
|
76
83
|
|
77
|
-
##
|
78
|
-
|
79
|
-
```ruby
|
80
|
-
# config/initializers/console.rb
|
81
|
-
|
82
|
-
# helper definitions go here...
|
83
|
-
|
84
|
-
RailsConsoleToolkit.install!
|
85
|
-
```
|
86
|
-
|
84
|
+
## Custom Helpers
|
87
85
|
|
88
86
|
### Generic helpers
|
89
87
|
|
90
88
|
```ruby
|
91
89
|
# config/initializers/console.rb
|
92
90
|
|
93
|
-
RailsConsoleToolkit.
|
94
|
-
:
|
91
|
+
RailsConsoleToolkit.configure do |config|
|
92
|
+
config.helper :foo do
|
93
|
+
:bar
|
94
|
+
end
|
95
95
|
end
|
96
96
|
```
|
97
97
|
|
@@ -106,7 +106,9 @@ end
|
|
106
106
|
```ruby
|
107
107
|
# config/initializers/console.rb
|
108
108
|
|
109
|
-
RailsConsoleToolkit.
|
109
|
+
RailsConsoleToolkit.configure do |config|
|
110
|
+
config.alias :r, :reload!
|
111
|
+
end
|
110
112
|
```
|
111
113
|
|
112
114
|
```
|
@@ -120,7 +122,9 @@ RailsConsoleToolkit.alias :r, :reload!
|
|
120
122
|
```ruby
|
121
123
|
# config/initializers/console.rb
|
122
124
|
|
123
|
-
RailsConsoleToolkit.
|
125
|
+
RailsConsoleToolkit.configure do |config|
|
126
|
+
config.model_helper 'Spree::Product', as: :product, by: %i[:name, :slug]
|
127
|
+
end
|
124
128
|
```
|
125
129
|
|
126
130
|
|
@@ -138,10 +142,11 @@ RailsConsoleToolkit.model_helper 'Spree::Product', as: :product, by: %i[:name, :
|
|
138
142
|
```ruby
|
139
143
|
# config/initializers/console.rb
|
140
144
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
+
RailsConsoleToolkit.configure do |config|
|
146
|
+
# Will define an alias :x for "exit"
|
147
|
+
config.use_pack :aliases
|
148
|
+
config.remove_helper :x
|
149
|
+
end
|
145
150
|
```
|
146
151
|
|
147
152
|
```
|
@@ -1,2 +1,2 @@
|
|
1
1
|
RailsConsoleToolkit.alias_helper :x, :exit
|
2
|
-
RailsConsoleToolkit.alias_helper :r, :reload
|
2
|
+
RailsConsoleToolkit.alias_helper :r, :reload!
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "active_support/core_ext/string/inflections"
|
2
|
+
|
3
|
+
module RailsConsoleToolkit::Helpers
|
4
|
+
def use_pack pack_name
|
5
|
+
unless %w[aliases solidus utils].include? pack_name.to_s
|
6
|
+
raise Error, "unknown pack name: #{pack_name}"
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rails_console_toolkit/#{pack_name}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def install!(target_module = Rails::ConsoleMethods)
|
13
|
+
helper_methods.each do |name, block|
|
14
|
+
target_module.define_method(name, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def helper(name, &block)
|
19
|
+
helper_methods[name.to_sym] = block
|
20
|
+
end
|
21
|
+
|
22
|
+
def model_helper(klass, as: nil, by: nil, cached: true)
|
23
|
+
klass = klass.constantize if klass.respond_to? :constantize
|
24
|
+
method_name = as || klass.name.gsub('::', '_').underscore
|
25
|
+
cache_key = method_name
|
26
|
+
attribute_names = by || []
|
27
|
+
|
28
|
+
record = nil # use the closure as a cache
|
29
|
+
|
30
|
+
helper method_name do |key = nil|
|
31
|
+
record = nil unless cached
|
32
|
+
|
33
|
+
if key
|
34
|
+
record = nil
|
35
|
+
record ||= klass.find(key) if Numeric === key
|
36
|
+
attribute_names.find { |name| record ||= klass.find_by(name => key) }
|
37
|
+
end
|
38
|
+
|
39
|
+
record
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def alias_helper(new_name, old_name)
|
44
|
+
helper(new_name) { send(old_name) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def remove_helper(name)
|
48
|
+
helper_methods.delete(name.to_sym)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def helper_methods
|
54
|
+
@helper_methods ||= {}
|
55
|
+
end
|
56
|
+
end
|
@@ -1,55 +1,19 @@
|
|
1
1
|
require "rails_console_toolkit/version"
|
2
|
-
require "active_support/core_ext/string/inflections"
|
3
2
|
|
4
3
|
module RailsConsoleToolkit
|
5
|
-
|
4
|
+
autoload :Helpers, "rails_console_toolkit/helpers"
|
6
5
|
|
7
6
|
class Error < StandardError; end
|
8
7
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
helper_methods.each do |name, block|
|
13
|
-
target_module.define_method(name, &block)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def helper(name, &block)
|
18
|
-
helper_methods[name.to_sym] = block
|
8
|
+
def self.setup
|
9
|
+
extend Helpers
|
19
10
|
end
|
20
11
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
record = nil # use the closure as a cache
|
28
|
-
|
29
|
-
helper method_name do |key = nil|
|
30
|
-
record = nil unless cached
|
31
|
-
|
32
|
-
if key
|
33
|
-
record = nil
|
34
|
-
record ||= klass.find(key) if Numeric === key
|
35
|
-
attribute_names.find { |name| record ||= klass.find_by(name => key) }
|
36
|
-
end
|
37
|
-
|
38
|
-
record
|
12
|
+
def self.configure(&block)
|
13
|
+
Rails.application.console do
|
14
|
+
setup
|
15
|
+
block.call(self)
|
16
|
+
install!
|
39
17
|
end
|
40
18
|
end
|
41
|
-
|
42
|
-
def alias_helper(new_name, old_name)
|
43
|
-
helper(new_name) { send(old_name) }
|
44
|
-
end
|
45
|
-
|
46
|
-
def remove_helper(name)
|
47
|
-
helper_methods.delete(name.to_sym)
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def helper_methods
|
53
|
-
@helper_methods ||= {}
|
54
|
-
end
|
55
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_console_toolkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Elia Schito
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- bin/setup
|
86
86
|
- lib/rails_console_toolkit.rb
|
87
87
|
- lib/rails_console_toolkit/aliases.rb
|
88
|
+
- lib/rails_console_toolkit/helpers.rb
|
88
89
|
- lib/rails_console_toolkit/solidus.rb
|
89
90
|
- lib/rails_console_toolkit/utils.rb
|
90
91
|
- lib/rails_console_toolkit/version.rb
|