dark_finger 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +8 -142
- data/docs/migration_constants.md +59 -0
- data/docs/model_structure.md +145 -0
- data/lib/dark_finger/version.rb +1 -1
- data/lib/rubocop/cop/dark_finger/active_model_node_decorator.rb +4 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23fb62829315ff2c6ee77ff2d0731948250480c1bdf23abcb97efb85c23139b7
|
4
|
+
data.tar.gz: 9959e4a1995a033feab1361ee74b32a3a948767654bb81fce6687d76f40c1fdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30d0f1666e440e035521415e3be596880725f286b2fbbd49fc342c4c34630ddc8ea3c4574d532c8118707c02a6edc8d4394275aff3c333feea4a4e91446edb99
|
7
|
+
data.tar.gz: 61e46f1213ff59c53de7ae8fad863a68d1c4e7f9f307a3392058ab1b5409d0a4fb2f8cab881f27012667ac5332fb729916594277129b3175f934db86a4a21087
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,68 +1,13 @@
|
|
1
1
|
![Dark Finger](.github/dark_finger.jpg "Dark Finger")
|
2
2
|
|
3
|
-
A Rubocop extension
|
3
|
+
A Rubocop extension containing two cops:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
inconsistent across files.
|
5
|
+
1. [Model Structure](docs/model_structure.md). Keep those "macro" methods at the top
|
6
|
+
of your model files clean and consistent across the project.
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
1. Aren't grouped together
|
13
|
-
2. The groups don't appear in the right order
|
14
|
-
3. The groups aren't commented properly
|
15
|
-
|
16
|
-
This is the kind of model file that we like. Notice how all the model elements
|
17
|
-
are grouped, commented, and ordered (although ordering is not visible from just
|
18
|
-
one file):
|
19
|
-
|
20
|
-
```ruby
|
21
|
-
class Horse < ApplicationRecord
|
22
|
-
## Includes ##
|
23
|
-
include GallopingMagicPowers
|
24
|
-
include LazerEyes
|
25
|
-
|
26
|
-
## Enums ##
|
27
|
-
enum breed: %i[thoroughbred
|
28
|
-
arabian
|
29
|
-
american quarter horse
|
30
|
-
clydesdale
|
31
|
-
mustang]
|
32
|
-
|
33
|
-
## Associations ##
|
34
|
-
has_many :legs
|
35
|
-
belongs_to :brain
|
36
|
-
belongs_to :saddle
|
37
|
-
|
38
|
-
## Validations ##
|
39
|
-
validates_presence_of :breed
|
40
|
-
validates_presence_of :age
|
41
|
-
|
42
|
-
## Scopes ##
|
43
|
-
scope :dead, -> { where(state: 'dead') }
|
44
|
-
scope :alive, -> { where(state: 'alive') }
|
45
|
-
|
46
|
-
## Attributes ##
|
47
|
-
attr_accessor :promote_to_demon_horse
|
48
|
-
|
49
|
-
## Callbacks ##
|
50
|
-
after_save :callbacks_are_evil_you_should_be_ashamed
|
51
|
-
|
52
|
-
## Misc ##
|
53
|
-
serialize :nose_hairs, Array
|
54
|
-
acts_as_taggable
|
55
|
-
|
56
|
-
def self.foobario
|
57
|
-
# foo
|
58
|
-
end
|
59
|
-
|
60
|
-
def gallop_hard
|
61
|
-
# ...
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
```
|
8
|
+
2. [Migration Constants](docs/migration_constants.md). Prevent a common source of
|
9
|
+
migration breakage - dependencies on things that are likely to change by the
|
10
|
+
time the migration is actually run.
|
66
11
|
|
67
12
|
## Installation
|
68
13
|
|
@@ -80,88 +25,9 @@ Or install it yourself as:
|
|
80
25
|
|
81
26
|
$ gem install dark_finger
|
82
27
|
|
83
|
-
## Usage
|
84
|
-
|
85
|
-
Install the gem. Then, in your `.rubycop.yml` file, require `dark_finger` and
|
86
|
-
add your desired config.
|
87
|
-
|
88
|
-
For example:
|
89
|
-
|
90
|
-
```yaml
|
91
|
-
# in .rubocop.yml
|
92
|
-
|
93
|
-
# this is required
|
94
|
-
require: dark_finger
|
95
|
-
|
96
|
-
DarkFinger/ModelStructure:
|
97
|
-
|
98
|
-
# this is also required
|
99
|
-
Include:
|
100
|
-
- 'app/models/*'
|
101
|
-
|
102
|
-
# specify the order that the model elements must appear in
|
103
|
-
required_order:
|
104
|
-
- module
|
105
|
-
- include
|
106
|
-
- enum
|
107
|
-
- constant
|
108
|
-
- association
|
109
|
-
- validation
|
110
|
-
- scope
|
111
|
-
- attributes
|
112
|
-
- callback
|
113
|
-
- misc
|
114
|
-
- constructor
|
115
|
-
- class_method
|
116
|
-
- instance_method
|
117
|
-
|
118
|
-
# specify the comments that must appear above each group of model elements
|
119
|
-
required_comments:
|
120
|
-
association: '## Relationships ##'
|
121
|
-
attribute: '## Attributes ##'
|
122
|
-
callback: '## Callbacks ##'
|
123
|
-
constant: '## Constants ##'
|
124
|
-
enum: '## Enums ##'
|
125
|
-
include: '## Includes ##'
|
126
|
-
misc: '## Misc ##'
|
127
|
-
module: '## Modules ##'
|
128
|
-
scope: '## Scopes ##'
|
129
|
-
validation: '## Validations ##'
|
130
|
-
|
131
|
-
# specify the methods that are categorized as "misc"
|
132
|
-
misc_method_names:
|
133
|
-
- acts_as_list
|
134
|
-
- acts_as_taggable
|
135
|
-
- friendly_id
|
136
|
-
- serialize
|
137
|
-
- workflow
|
138
|
-
```
|
139
|
-
|
140
|
-
Supported model elements:
|
141
|
-
|
142
|
-
|
143
|
-
| Config key | Description (when not obvious) |
|
144
|
-
|-----------------|--------------------------------------------|
|
145
|
-
| association | |
|
146
|
-
| attribute | `attr_reader` and friends |
|
147
|
-
| callback | `after_save` et al. |
|
148
|
-
| class_method | |
|
149
|
-
| constant | |
|
150
|
-
| constructor | |
|
151
|
-
| enum | |
|
152
|
-
| include | |
|
153
|
-
| instance_method | |
|
154
|
-
| misc | This is a configurable set of method calls |
|
155
|
-
| module | Any `module Foo; ...; end` declarations |
|
156
|
-
| scope | Any `scope` or `default_scope` calls |
|
157
|
-
| validation | |
|
158
|
-
|
159
|
-
|
160
|
-
Any model elements that are not included in "required order" will be ignored.
|
28
|
+
## Usage
|
161
29
|
|
162
|
-
|
163
|
-
future, but for now we have just agreed that anything goes in the private
|
164
|
-
section of our models.
|
30
|
+
See the guides for each of the Cops for details.
|
165
31
|
|
166
32
|
## License
|
167
33
|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# The Migration Constants Cop
|
2
|
+
|
3
|
+
In our rails migration files we don't want dependencies on, for example,
|
4
|
+
ActiveRecord model files.
|
5
|
+
|
6
|
+
This is because migration files should be "timeless" and able to run at any
|
7
|
+
point in the future. Our model files change very frequently - and therefore
|
8
|
+
cannot be depended on directly. We must redeclare the model inside the
|
9
|
+
migration file.
|
10
|
+
|
11
|
+
For example:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
# BAD :'(
|
15
|
+
#
|
16
|
+
# This migration depends on `SomeModel` and `.some_scope`. When this
|
17
|
+
# migration is actually run, either of those things could have changed name,
|
18
|
+
# or perhaps `some_scope` might behave differently by then or have been
|
19
|
+
# deleted.
|
20
|
+
class FooMigration < ActiveRecord::Migration[5.1]
|
21
|
+
def up
|
22
|
+
SomeModel.some_scope.each do
|
23
|
+
# stuff
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# GOOD :-D
|
29
|
+
#
|
30
|
+
# This migration has no external dependencies on our app. It is unlikely to
|
31
|
+
# break in the future as our app changes.
|
32
|
+
class FooMigration < ActiveRecord::Migration[5.1]
|
33
|
+
class SomeModel < ActiveRecord::Base
|
34
|
+
scope :some_scope, -> { ... }
|
35
|
+
end
|
36
|
+
|
37
|
+
def up
|
38
|
+
SomeModel.some_scope.each do
|
39
|
+
# stuff
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
This cop will issue warnings if a migration file depends on certain constants
|
46
|
+
(like model files) that it doesn't declare.
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
Install the gem and then add this to your `.rubocop.yml` file:
|
51
|
+
|
52
|
+
```yaml
|
53
|
+
# this is required
|
54
|
+
require: dark_finger
|
55
|
+
|
56
|
+
DarkFinger/MigrationConstants:
|
57
|
+
Include:
|
58
|
+
- 'db/migrate/*.rb'
|
59
|
+
```
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# The Model Structure Cop
|
2
|
+
|
3
|
+
At work we've found that, as our model files grow in size, there are many
|
4
|
+
"macro" methods (scopes, validations, etc) at the top that become messy and
|
5
|
+
inconsistent across files.
|
6
|
+
|
7
|
+
This cop will issue warnings if the various model elements:
|
8
|
+
|
9
|
+
1. Aren't grouped together
|
10
|
+
2. The groups don't appear in the right order
|
11
|
+
3. The groups aren't commented properly
|
12
|
+
|
13
|
+
This is the kind of model file that we like. Notice how all the model elements
|
14
|
+
are grouped, commented, and ordered (although ordering is not visible from just
|
15
|
+
one file):
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class Horse < ApplicationRecord
|
19
|
+
## Includes ##
|
20
|
+
include GallopingMagicPowers
|
21
|
+
include LazerEyes
|
22
|
+
|
23
|
+
## Enums ##
|
24
|
+
enum breed: %i[thoroughbred
|
25
|
+
arabian
|
26
|
+
american quarter horse
|
27
|
+
clydesdale
|
28
|
+
mustang]
|
29
|
+
|
30
|
+
## Associations ##
|
31
|
+
has_many :legs
|
32
|
+
belongs_to :brain
|
33
|
+
belongs_to :saddle
|
34
|
+
|
35
|
+
## Validations ##
|
36
|
+
validates_presence_of :breed
|
37
|
+
validates_presence_of :age
|
38
|
+
|
39
|
+
## Scopes ##
|
40
|
+
scope :dead, -> { where(state: 'dead') }
|
41
|
+
scope :alive, -> { where(state: 'alive') }
|
42
|
+
|
43
|
+
## Attributes ##
|
44
|
+
attr_accessor :promote_to_demon_horse
|
45
|
+
|
46
|
+
## Callbacks ##
|
47
|
+
after_save :callbacks_are_evil_you_should_be_ashamed
|
48
|
+
|
49
|
+
## Misc ##
|
50
|
+
serialize :nose_hairs, Array
|
51
|
+
acts_as_taggable
|
52
|
+
|
53
|
+
def self.foobario
|
54
|
+
# foo
|
55
|
+
end
|
56
|
+
|
57
|
+
def gallop_hard
|
58
|
+
# ...
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
```
|
63
|
+
|
64
|
+
## Usage and Configuration
|
65
|
+
|
66
|
+
Install the gem. Then, in your `.rubycop.yml` file, require `dark_finger` and
|
67
|
+
add your desired config.
|
68
|
+
|
69
|
+
For example:
|
70
|
+
|
71
|
+
```yaml
|
72
|
+
# in .rubocop.yml
|
73
|
+
|
74
|
+
# this is required
|
75
|
+
require: dark_finger
|
76
|
+
|
77
|
+
DarkFinger/ModelStructure:
|
78
|
+
|
79
|
+
# this is also required
|
80
|
+
Include:
|
81
|
+
- 'app/models/*'
|
82
|
+
|
83
|
+
# specify the order that the model elements must appear in
|
84
|
+
required_order:
|
85
|
+
- module
|
86
|
+
- include
|
87
|
+
- enum
|
88
|
+
- constant
|
89
|
+
- association
|
90
|
+
- validation
|
91
|
+
- scope
|
92
|
+
- attributes
|
93
|
+
- callback
|
94
|
+
- misc
|
95
|
+
- constructor
|
96
|
+
- class_method
|
97
|
+
- instance_method
|
98
|
+
|
99
|
+
# specify the comments that must appear above each group of model elements
|
100
|
+
required_comments:
|
101
|
+
association: '## Relationships ##'
|
102
|
+
attribute: '## Attributes ##'
|
103
|
+
callback: '## Callbacks ##'
|
104
|
+
constant: '## Constants ##'
|
105
|
+
enum: '## Enums ##'
|
106
|
+
include: '## Includes ##'
|
107
|
+
misc: '## Misc ##'
|
108
|
+
module: '## Modules ##'
|
109
|
+
scope: '## Scopes ##'
|
110
|
+
validation: '## Validations ##'
|
111
|
+
|
112
|
+
# specify the methods that are categorized as "misc"
|
113
|
+
misc_method_names:
|
114
|
+
- acts_as_list
|
115
|
+
- acts_as_taggable
|
116
|
+
- friendly_id
|
117
|
+
- serialize
|
118
|
+
- workflow
|
119
|
+
```
|
120
|
+
|
121
|
+
Supported model elements:
|
122
|
+
|
123
|
+
|
124
|
+
| Config key | Description (when not obvious) |
|
125
|
+
|-----------------|--------------------------------------------|
|
126
|
+
| association | |
|
127
|
+
| attribute | `attr_reader` and friends |
|
128
|
+
| callback | `after_save` et al. |
|
129
|
+
| class_method | |
|
130
|
+
| constant | |
|
131
|
+
| constructor | |
|
132
|
+
| enum | |
|
133
|
+
| include | |
|
134
|
+
| instance_method | |
|
135
|
+
| misc | This is a configurable set of method calls |
|
136
|
+
| module | Any `module Foo; ...; end` declarations |
|
137
|
+
| scope | Any `scope` or `default_scope` calls |
|
138
|
+
| validation | |
|
139
|
+
|
140
|
+
|
141
|
+
Any model elements that are not included in "required order" will be ignored.
|
142
|
+
|
143
|
+
Dark Finger ignores everything that appears after `private`. This may change in
|
144
|
+
future, but for now we have just agreed that anything goes in the private
|
145
|
+
section of our models.
|
data/lib/dark_finger/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
1
3
|
module RuboCop
|
2
4
|
module Cop
|
3
5
|
module DarkFinger
|
@@ -61,7 +63,8 @@ module RuboCop
|
|
61
63
|
end
|
62
64
|
|
63
65
|
def private_declaration?
|
64
|
-
|
66
|
+
# need to check respond_to since this may not be called only "on_send"
|
67
|
+
respond_to?(:method_name) && method_name == :private && receiver.nil?
|
65
68
|
end
|
66
69
|
|
67
70
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dark_finger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Professor Wang Matrix PhD
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2019-01-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -100,6 +100,8 @@ files:
|
|
100
100
|
- bin/console
|
101
101
|
- bin/setup
|
102
102
|
- dark_finger.gemspec
|
103
|
+
- docs/migration_constants.md
|
104
|
+
- docs/model_structure.md
|
103
105
|
- lib/dark_finger.rb
|
104
106
|
- lib/dark_finger/version.rb
|
105
107
|
- lib/rubocop/cop/dark_finger/active_model_node_decorator.rb
|