|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module Rails |
| 6 | + # Looks for `accepts_nested_attributes_for` attributes writers that don't |
| 7 | + # specify an `:update_only` option. |
| 8 | + # |
| 9 | + # @example |
| 10 | + # # bad |
| 11 | + # class Member < ActiveRecord::Base |
| 12 | + # has_one :avatar |
| 13 | + # accepts_nested_attributes_for :avatar |
| 14 | + # end |
| 15 | + # |
| 16 | + # # good |
| 17 | + # class Member < ActiveRecord::Base |
| 18 | + # has_one :avatar |
| 19 | + # accepts_nested_attributes_for :avatar, update_only: true |
| 20 | + # end |
| 21 | + class AcceptsNestedAttributesForUpdateOnly < Base |
| 22 | + MSG = 'Specify a `:update_only` option.' |
| 23 | + RESTRICT_ON_SEND = %i[accepts_nested_attributes_for].freeze |
| 24 | + |
| 25 | + def_node_search :active_resource_class?, <<~PATTERN |
| 26 | + (const (const {nil? cbase} :ActiveResource) :Base) |
| 27 | + PATTERN |
| 28 | + |
| 29 | + def_node_matcher :accepts_nested_attributes_for_without_options?, <<~PATTERN |
| 30 | + (send _ {:accepts_nested_attributes_for} _) |
| 31 | + PATTERN |
| 32 | + |
| 33 | + def_node_matcher :accepts_nested_attributes_for_with_options?, <<~PATTERN |
| 34 | + (send _ {:accepts_nested_attributes_for} ... (hash $...)) |
| 35 | + PATTERN |
| 36 | + |
| 37 | + def_node_matcher :update_only_option?, <<~PATTERN |
| 38 | + (pair (sym :update_only) {!nil (nil)}) |
| 39 | + PATTERN |
| 40 | + |
| 41 | + def_node_matcher :with_options_block, <<~PATTERN |
| 42 | + (block |
| 43 | + (send nil? :with_options |
| 44 | + (hash $...)) |
| 45 | + (args) ...) |
| 46 | + PATTERN |
| 47 | + |
| 48 | + def_node_matcher :accepts_nested_attributes_for_extension_block?, <<~PATTERN |
| 49 | + (block |
| 50 | + (send nil? :accepts_nested_attributes_for _) |
| 51 | + (args) ...) |
| 52 | + PATTERN |
| 53 | + |
| 54 | + def on_send(node) |
| 55 | + return if active_resource?(node.parent) |
| 56 | + return if !accepts_nested_attributes_for_without_options?(node) && \ |
| 57 | + valid_options?(accepts_nested_attributes_for_with_options?(node)) |
| 58 | + return if valid_options_in_with_options_block?(node) |
| 59 | + |
| 60 | + add_offense(node.loc.selector) |
| 61 | + end |
| 62 | + |
| 63 | + private |
| 64 | + |
| 65 | + def valid_options_in_with_options_block?(node) |
| 66 | + return true unless node.parent |
| 67 | + |
| 68 | + n = node.parent.begin_type? |
| 69 | + n ||= accepts_nested_attributes_for_extension_block?(node.parent) ? node.parent.parent : node.parent |
| 70 | + |
| 71 | + contain_valid_options_in_with_options_block?(n) |
| 72 | + end |
| 73 | + |
| 74 | + def contain_valid_options_in_with_options_block?(node) |
| 75 | + if (options = with_options_block(node)) |
| 76 | + return true if valid_options?(options) |
| 77 | + |
| 78 | + return false unless node.parent |
| 79 | + |
| 80 | + return true if contain_valid_options_in_with_options_block?(node.parent.parent) |
| 81 | + end |
| 82 | + |
| 83 | + false |
| 84 | + end |
| 85 | + |
| 86 | + def valid_options?(options) |
| 87 | + return false if options.nil? |
| 88 | + |
| 89 | + options = extract_option_if_kwsplat(options) |
| 90 | + |
| 91 | + return true unless options |
| 92 | + return true if options.any? do |o| |
| 93 | + update_only_option?(o) |
| 94 | + end |
| 95 | + |
| 96 | + false |
| 97 | + end |
| 98 | + |
| 99 | + def extract_option_if_kwsplat(options) |
| 100 | + if options.first.kwsplat_type? && options.first.children.first.hash_type? |
| 101 | + return options.first.children.first.pairs |
| 102 | + end |
| 103 | + |
| 104 | + options |
| 105 | + end |
| 106 | + |
| 107 | + def active_resource?(node) |
| 108 | + return false if node.nil? |
| 109 | + |
| 110 | + active_resource_class?(node) |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | +end |
0 commit comments