mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 02:57:57 +00:00 
			
		
		
		
	fix: Add custom rubocop rules for email fetch (#8987)
This commit is contained in:
		
							
								
								
									
										12
									
								
								.rubocop.yml
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								.rubocop.yml
									
									
									
									
									
								
							| @@ -2,6 +2,8 @@ require: | |||||||
|   - rubocop-performance |   - rubocop-performance | ||||||
|   - rubocop-rails |   - rubocop-rails | ||||||
|   - rubocop-rspec |   - rubocop-rspec | ||||||
|  |   - ./rubocop/use_from_email.rb | ||||||
|  |   - ./rubocop/custom_cop_location.rb | ||||||
|  |  | ||||||
| Layout/LineLength: | Layout/LineLength: | ||||||
|   Max: 150 |   Max: 150 | ||||||
| @@ -140,6 +142,16 @@ RSpec/MultipleExpectations: | |||||||
| RSpec/MultipleMemoizedHelpers: | RSpec/MultipleMemoizedHelpers: | ||||||
|   Max: 14 |   Max: 14 | ||||||
|  |  | ||||||
|  | # custom rules | ||||||
|  | UseFromEmail: | ||||||
|  |   Enabled: true | ||||||
|  |   Exclude: | ||||||
|  |     - 'app/models/user.rb' | ||||||
|  |     - 'app/models/contact.rb' | ||||||
|  |  | ||||||
|  | CustomCopLocation: | ||||||
|  |   Enabled: true | ||||||
|  |  | ||||||
| AllCops: | AllCops: | ||||||
|   NewCops: enable |   NewCops: enable | ||||||
|   Exclude: |   Exclude: | ||||||
|   | |||||||
							
								
								
									
										18
									
								
								rubocop/custom_cop_location.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								rubocop/custom_cop_location.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | require 'rubocop' | ||||||
|  |  | ||||||
|  | # enforce rubocop custom rules to only be added in the `rubocop` directory | ||||||
|  | class CustomCopLocation < RuboCop::Cop::Base | ||||||
|  |   MSG = 'Custom cops should be added in the `rubocop` directory.'.freeze | ||||||
|  |  | ||||||
|  |   def on_send(node) | ||||||
|  |     return unless node.source.include?('require \'rubocop\'') | ||||||
|  |  | ||||||
|  |     # convert the full path to relative | ||||||
|  |     full_path = processed_source.file_path | ||||||
|  |     relative_path = Pathname.new(full_path).relative_path_from(Pathname.new(Dir.pwd)).to_s | ||||||
|  |  | ||||||
|  |     return if relative_path.start_with?('rubocop') | ||||||
|  |  | ||||||
|  |     add_offense(node, message: MSG) | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										16
									
								
								rubocop/use_from_email.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								rubocop/use_from_email.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | |||||||
|  | require 'rubocop' | ||||||
|  |  | ||||||
|  | # Enforces use of from_email for email attribute lookups | ||||||
|  | class UseFromEmail < RuboCop::Cop::Base | ||||||
|  |   MSG = 'Use `from_email` for email lookups to ensure case insensitivity.'.freeze | ||||||
|  |  | ||||||
|  |   def_node_matcher :find_by_email?, <<~PATTERN | ||||||
|  |     (send _ :find_by (hash (pair (sym :email) _))) | ||||||
|  |   PATTERN | ||||||
|  |  | ||||||
|  |   def on_send(node) | ||||||
|  |     return unless find_by_email?(node) | ||||||
|  |  | ||||||
|  |     add_offense(node, message: MSG) | ||||||
|  |   end | ||||||
|  | end | ||||||
		Reference in New Issue
	
	Block a user
	 Shivam Mishra
					Shivam Mishra