rails_console_toolkit 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +175 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rails_console_toolkit.rb +57 -0
- data/lib/rails_console_toolkit/aliases.rb +2 -0
- data/lib/rails_console_toolkit/solidus.rb +13 -0
- data/lib/rails_console_toolkit/utils.rb +14 -0
- data/lib/rails_console_toolkit/version.rb +3 -0
- data/rails_console_toolkit.gemspec +35 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c63ae0fa9d97e7519b9e84c9ccaec90f2be8f5cfef5e8b25729debfe5d0dd374
|
4
|
+
data.tar.gz: 8de43d13ff473dee04f9130f9d22497412a3a67dc77fa7d7bdfb2fbfaefa94c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa3fe513e335e98f6c498969dc442847533efffa87536801e3bdcc3626c6f4e0b3b19fa87fedfd436b4a8f9d2da3e8f2fa2636b0913c237a84d071215f9b98f5
|
7
|
+
data.tar.gz: d9e67100e2b6619bf69f8ce170a6e7325cd85e20e2109eefe479869f1b416c2ebe06a83a61ee44b7af921e7c16191d1fe3f925b4a89d26150349db1a6c47dc3b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Elia Schito
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
# RailsConsoleToolkit 🔧🧰
|
2
|
+
|
3
|
+
*Configurable Rails Console Helpers*
|
4
|
+
|
5
|
+
Find records faster, add custom helpers, improve your console life by 100%.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rails_console_toolkit'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rails_console_toolkit
|
22
|
+
|
23
|
+
|
24
|
+
## Helper packs
|
25
|
+
|
26
|
+
### Aliases
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
# config/initializers/console.rb
|
30
|
+
|
31
|
+
require 'rails_console_toolkit/aliases'
|
32
|
+
```
|
33
|
+
|
34
|
+
```
|
35
|
+
> x # alias exit
|
36
|
+
> r # alias reload!
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
### Utils
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# config/initializers/console.rb
|
44
|
+
|
45
|
+
require 'rails_console_toolkit/aliases'
|
46
|
+
```
|
47
|
+
|
48
|
+
```
|
49
|
+
> benchmark("foo") { sleep 3 }
|
50
|
+
foo (3000.6ms)
|
51
|
+
=> 3
|
52
|
+
|
53
|
+
> bm # alias benchmark
|
54
|
+
```
|
55
|
+
|
56
|
+
|
57
|
+
### [Solidus](https://solidus.io)
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# config/initializers/console.rb
|
61
|
+
|
62
|
+
require 'rails_console_toolkit/aliases'
|
63
|
+
```
|
64
|
+
|
65
|
+
```
|
66
|
+
> load_factories # will load solidus factories, useful to create dummy data in development
|
67
|
+
> product(...) # => will look for Spree::Product records by :id, :slug, :name
|
68
|
+
> variant(...) # => will look for Spree::Variant records by :id, :sku
|
69
|
+
> taxon(...) # => will look for Spree::Taxon records by :id, :permalink
|
70
|
+
> order(...) # => will look for Spree::Order records by :id, :number
|
71
|
+
> user(...) # => will look for Spree::User records by :id, :email
|
72
|
+
> country(...) # => will look for Spree::Country records by :id, :iso, :iso3, :iso_name, :name
|
73
|
+
```
|
74
|
+
|
75
|
+
## Usage
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# config/initializers/console.rb
|
79
|
+
|
80
|
+
# helper definitions go here...
|
81
|
+
|
82
|
+
RailsConsoleToolkit.install!
|
83
|
+
```
|
84
|
+
|
85
|
+
|
86
|
+
### Generic helpers
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
# config/initializers/console.rb
|
90
|
+
|
91
|
+
RailsConsoleToolkit.helper :foo do
|
92
|
+
:bar
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
```
|
97
|
+
# bin/rails console
|
98
|
+
|
99
|
+
> foo # => :bar
|
100
|
+
```
|
101
|
+
|
102
|
+
### Aliases
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
# config/initializers/console.rb
|
106
|
+
|
107
|
+
RailsConsoleToolkit.alias :r, :reload!
|
108
|
+
```
|
109
|
+
|
110
|
+
```
|
111
|
+
# bin/rails console
|
112
|
+
|
113
|
+
> r # The same as typing "reload!"
|
114
|
+
```
|
115
|
+
|
116
|
+
### Model helpers
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
# config/initializers/console.rb
|
120
|
+
|
121
|
+
RailsConsoleToolkit.model_helper 'Spree::Product', as: :product, by: %i[:name, :slug]
|
122
|
+
```
|
123
|
+
|
124
|
+
|
125
|
+
```
|
126
|
+
# bin/rails console
|
127
|
+
|
128
|
+
> product('black-tshirt') # => #<Spree::Product id: 123, name: "Black T-Shirt", slug: "black-tshirt", …>
|
129
|
+
> product.slug # => "black-tshirt"
|
130
|
+
> product 456 # => #<Spree::Product id: 456, name: "Red T-Shirt", slug: "red-tshirt", …>
|
131
|
+
> product.slug # => "red-tshirt"
|
132
|
+
```
|
133
|
+
|
134
|
+
### Removing unwanted helpers
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
# config/initializers/console.rb
|
138
|
+
|
139
|
+
require 'rails_console_toolkit/aliases' # Will define an alias :x for "exit"
|
140
|
+
|
141
|
+
RailsConsoleToolkit.remove_helper :x
|
142
|
+
RailsConsoleToolkit.install!
|
143
|
+
```
|
144
|
+
|
145
|
+
```
|
146
|
+
# bin/rails console
|
147
|
+
|
148
|
+
> x # NameError (undefined local variable or method \`x' for main:Object)
|
149
|
+
```
|
150
|
+
|
151
|
+
## Development
|
152
|
+
|
153
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
154
|
+
|
155
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
156
|
+
|
157
|
+
## Contributing
|
158
|
+
|
159
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/elia/rails_console_toolkit.
|
160
|
+
|
161
|
+
## License
|
162
|
+
|
163
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
164
|
+
|
165
|
+
## About
|
166
|
+
|
167
|
+
[![Nebulab][nebulab-logo]][nebulab]
|
168
|
+
|
169
|
+
This project is funded and maintained by the [Nebulab][nebulab] team.
|
170
|
+
|
171
|
+
We firmly believe in the power of open-source. [Contact us][contact-us] if you
|
172
|
+
like our work and you need help with your project design or development.
|
173
|
+
|
174
|
+
[nebulab]: http://nebulab.it/
|
175
|
+
[nebulab-logo]: http://nebulab.it/assets/images/public/logo.svg
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rails_console_toolkit"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "rails_console_toolkit/version"
|
2
|
+
require "active_support/core_ext/string/inflections"
|
3
|
+
|
4
|
+
module RailsConsoleToolkit
|
5
|
+
extend self
|
6
|
+
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
def install!
|
10
|
+
require 'rails/console/app'
|
11
|
+
|
12
|
+
helper_methods.each do |name, block|
|
13
|
+
Rails::ConsoleMethods.define_method(name, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def helper(name, &block)
|
18
|
+
helper_methods[name.to_sym] = block
|
19
|
+
end
|
20
|
+
|
21
|
+
def model_helper(klass, as: nil, by: nil, cached: true)
|
22
|
+
klass = klass.constantize if klass.respond_to? :constantize
|
23
|
+
method_name = as || klass.name.gsub('::', '_').underscore
|
24
|
+
cache_key = method_name
|
25
|
+
attribute_names = by || []
|
26
|
+
|
27
|
+
record = nil # use the closure as a cache
|
28
|
+
|
29
|
+
helper method_name do |key = nil|
|
30
|
+
unless cached
|
31
|
+
record = nil
|
32
|
+
raise("missing key for #{key}") unless key
|
33
|
+
end
|
34
|
+
|
35
|
+
if key
|
36
|
+
record ||= klass.find(key) if key.is_a? Numeric
|
37
|
+
attribute_names.find { |name| record ||= klass.find_by(name => key) }
|
38
|
+
end
|
39
|
+
|
40
|
+
record
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def alias_helper(new_name, old_name)
|
45
|
+
helper(new_name) { send(old_name) }
|
46
|
+
end
|
47
|
+
|
48
|
+
def remove_helper(name)
|
49
|
+
helper_methods.delete(name.to_sym)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def helper_methods
|
55
|
+
@helper_methods ||= {}
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
RailsConsoleToolkit.helper :load_factories do
|
2
|
+
require 'factory_bot'
|
3
|
+
require 'spree/testing_support/factories'
|
4
|
+
FactoryBot.find_definitions
|
5
|
+
Rails::ConsoleMethods.send :include, ::FactoryBot::Syntax::Methods
|
6
|
+
end
|
7
|
+
|
8
|
+
RailsConsoleToolkit.model_helper Spree::Product, as: :product, by: [:slug, :name]
|
9
|
+
RailsConsoleToolkit.model_helper Spree::Variant, as: :variant, by: [:sku]
|
10
|
+
RailsConsoleToolkit.model_helper Spree::Taxon, as: :taxon, by: [:permalink]
|
11
|
+
RailsConsoleToolkit.model_helper Spree::Order, as: :order, by: [:number]
|
12
|
+
RailsConsoleToolkit.model_helper Spree::User, as: :user, by: [:email]
|
13
|
+
RailsConsoleToolkit.model_helper Spree::Country, as: :country, by: [:iso, :iso3, :iso_name, :name]
|
@@ -0,0 +1,14 @@
|
|
1
|
+
RailsConsoleToolkit.helper :benchmark do |*args,&block|
|
2
|
+
@benchmarker ||= begin
|
3
|
+
benchmarker = Object.new
|
4
|
+
def benchmarker.logger
|
5
|
+
Rails.logger
|
6
|
+
end
|
7
|
+
benchmarker.extend ActiveSupport::Benchmarkable
|
8
|
+
benchmarker
|
9
|
+
end
|
10
|
+
|
11
|
+
@benchmarker.benchmark(*args, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
RailsConsoleToolkit.alias_helper :bm, :benchmark
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "rails_console_toolkit/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_console_toolkit"
|
8
|
+
spec.version = RailsConsoleToolkit::VERSION
|
9
|
+
spec.authors = ["Elia Schito"]
|
10
|
+
spec.email = ["elia@schito.me"]
|
11
|
+
|
12
|
+
spec.summary = %q{Configurable Rails Console Helpers}
|
13
|
+
spec.description = %q{Find records faster, add custom helpers, improve your console life by 100%.}
|
14
|
+
spec.homepage = "https://github.com/nebulab/rails_console_toolkit#readme"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/nebulab/rails_console_toolkit"
|
19
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "rails", '>= 4'
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_console_toolkit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elia Schito
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: Find records faster, add custom helpers, improve your console life by
|
70
|
+
100%.
|
71
|
+
email:
|
72
|
+
- elia@schito.me
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/rails_console_toolkit.rb
|
86
|
+
- lib/rails_console_toolkit/aliases.rb
|
87
|
+
- lib/rails_console_toolkit/solidus.rb
|
88
|
+
- lib/rails_console_toolkit/utils.rb
|
89
|
+
- lib/rails_console_toolkit/version.rb
|
90
|
+
- rails_console_toolkit.gemspec
|
91
|
+
homepage: https://github.com/nebulab/rails_console_toolkit#readme
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
homepage_uri: https://github.com/nebulab/rails_console_toolkit#readme
|
96
|
+
source_code_uri: https://github.com/nebulab/rails_console_toolkit
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubygems_version: 3.0.3
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Configurable Rails Console Helpers
|
116
|
+
test_files: []
|