rubocop-rails-order_model_macros 0.1.2 → 0.1.21

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed903f2fbfeb0a0d31b12fd9065546096cbb7457
4
- data.tar.gz: 34add53f42543f33693587d283d72b820ac26d4e
3
+ metadata.gz: 9ba11eeb674a43a28a2fb1944e9cd5975e696f28
4
+ data.tar.gz: 4a0a2bd080367c4946085678da14a1b50835f161
5
5
  SHA512:
6
- metadata.gz: e1aa12a8c00effdfe02be99b18b1e44e9cd358e87f03d4532c9283becbb3fbdec5ba5d4d7a1cacac2b69266470cc695440b5c6d7386d08a41e580eadfcc3176a
7
- data.tar.gz: f10619faf332a82c35b004dd9a3d1e4f91937fa5ae4fbc782ee1597eb64d0b7ded8993b7d3d2285cfe6c893727a3f1a6f895fb87540f34276b618a774d8bace7
6
+ metadata.gz: fd091e969007c4a84a73681068938fb9c193f5ecddc3e821f75d0deee87aeca8f4189bad0af362e1b64a3c31e0e00445905fa07608c41fd773e0b9664b72aa6d
7
+ data.tar.gz: e6b52288501a7f7448cdc3c03558c7ca66e583d7bc71f476c9af68c4835ebb0fb72a0dab87e7d32b5ca4136d0a59cdd2ff9bc66d72b470e1c0f7eb063d49fb29
data/README.md CHANGED
@@ -12,15 +12,88 @@ gem 'rubocop-rails-order_model_macros'
12
12
 
13
13
  And then execute:
14
14
 
15
- $ bundle
15
+ ```shell
16
+ $ bundle
17
+ ```
16
18
 
17
19
  Or install it yourself as:
18
20
 
19
- $ gem install rubocop-rails-order_model_macros
21
+ ```shell
22
+ $ gem install rubocop-rails-order_model_macros
23
+ ```
20
24
 
21
25
  ## Usage
22
26
 
23
- TODO: Write usage instructions here
27
+ You need to tell RuboCop to load the `Rails/OrderModelMacros` extension. There are three ways to do this:
28
+
29
+ ### 1. RuboCop configuration file
30
+
31
+ Put this into your `.rubocop.yml`:
32
+
33
+ ```ruby
34
+ require: rubocop-rails-order_model_macros
35
+ ```
36
+
37
+ Now you can run `rubocop` and it will automatically load the `RuboCop` `Rails/OrderModelMacros` cop together with the standard cops.
38
+
39
+ ### 2. Command line
40
+
41
+ ```
42
+ rubocop --require rubocop-rails-order_model_macros
43
+ ```
44
+
45
+ ### 3. Rake task
46
+
47
+ ```ruby
48
+ RuboCop::RakeTask.new do |task|
49
+ task.requires << 'rubocop-rails-order_model_macros'
50
+ end
51
+ ```
52
+
53
+ ## Configuring the cops
54
+
55
+ The [default configuration](config/default.yml) defines an outer group ordering, Rails macros to check, and inner group orderings for associations and validations.
56
+
57
+ ```yaml
58
+ Rails/OrderModelMacros:
59
+ Description: 'Sort macros methos in Rails models.'
60
+ Enabled: true
61
+ PreferredGroupOrdering:
62
+ - default_scope
63
+ - class_method
64
+ - enum
65
+ - association
66
+ - validation
67
+ - callback
68
+ - delegate
69
+ - rails
70
+ - gem
71
+ - custom
72
+ - scope
73
+ PreferredInnerGroupOrdering:
74
+ association:
75
+ - belongs_to
76
+ - has_one
77
+ - has_and_belongs_to_many
78
+ - has_many
79
+ validation:
80
+ - validates
81
+ - validate
82
+ - with_options
83
+ Rails:
84
+ - accepts_nested_attributes_for
85
+ - serialize
86
+ - store_accessor
87
+ Custom: null
88
+ Gem: null
89
+ ```
90
+
91
+
92
+
93
+ All of these may be customized. To only include a subset of groups, or to modify the order, define your own `PreferredGroupOrdering`. To add additional Rails macros, define a `Rails` array. Definitions overwrite defaults, so if you customize, be sure to copy over any desired types from the default set when applicable.
94
+
95
+ - Add custom macros to `Custom`.
96
+ - Add any Gem defined macros to `Gem`.
24
97
 
25
98
  ## Development
26
99
 
@@ -32,7 +105,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
105
 
33
106
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubocop-rails-order_model_macros. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
107
 
35
-
36
108
  ## License
37
109
 
38
110
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -23,8 +23,7 @@ module RuboCop
23
23
  def on_class(node)
24
24
  _name, superclass, body = *node
25
25
 
26
- return unless body
27
- return unless body.begin_type?
26
+ return unless body && body.begin_type?
28
27
  return unless superclass && superclass.descendants.any?
29
28
  return unless %w(ActiveRecord ApplicationRecord).include?(superclass.descendants.first.const_name)
30
29
 
@@ -49,10 +48,14 @@ module RuboCop
49
48
  ordered_for_type = preferred_group_ordering[type]
50
49
  squeezed = squeeze(targets_for_type.map(&:method_name))
51
50
 
51
+ mixed = squeezed != squeezed.uniq
52
+
52
53
  a = (ordered_for_type & squeezed)
53
54
  b = (squeezed & ordered_for_type)
54
55
 
55
- a == b || (set_within_group_error_message(type, a, b) and false)
56
+ a = squeezed.drop_while { |el| squeezed.count(el) > 1 } if mixed
57
+
58
+ a == b or set_within_group_error_message(type, a, b) && false
56
59
  end
57
60
  end
58
61
 
@@ -70,7 +73,7 @@ module RuboCop
70
73
  a = type_order & squeezed
71
74
  b = squeezed & type_order
72
75
 
73
- a == b || (set_outer_group_error_message(a, b) and false)
76
+ a == b or set_outer_group_error_message(a, b) && false
74
77
  end
75
78
 
76
79
  def single_groups?(targets)
@@ -84,9 +87,12 @@ module RuboCop
84
87
  end
85
88
 
86
89
  def target_methods(body)
87
- body.children.compact.select do |child|
88
- child.send_type? && matches_targets?(child.method_name)
89
- end
90
+ body.children.compact.select(&method(:match)).map(&method(:match))
91
+ end
92
+
93
+ def match(child)
94
+ return child if child.send_type? && matches_targets?(child.method_name)
95
+ child.children && match(child.children.compact.first)
90
96
  end
91
97
 
92
98
  def matches_targets?(declared)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  module OrderModelMacros
6
6
  module Version
7
- STRING = "0.1.2".freeze
7
+ STRING = "0.1.21".freeze
8
8
 
9
9
  def self.gem_version
10
10
  Gem::Version.new(STRING)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails-order_model_macros
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caroline Artz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-17 00:00:00.000000000 Z
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop