def approve(approved_by,send_mail=true) self.is_approved = true if approved_by.is_a?(Fixnum) self.approved_by_id = approved_by else self.approved_by = approved_by end self.approved_at = Time.now if save and send_mail Jobs.enqueue(:user_email, type: :signup_after_approval, user_id: id, email_token: email_tokens.first.token ) # ... end end end describe ".approve" do before do user.approve(admin) end it "enqueues a 'signup after approval' email" do Jobs.expects(:enqueue).with( :user_email, has_entries(type: :signup_after_approval) ) end it "marks the user as approved" do expect(user).to be_approved end it "has the admin as the approved by" do expect(user.approved_by).to eq(admin) end it "has a value for approved_at" do expect(user.approved_at).to be_present end end discourse/discourse - app/models/user.rb
true if approved_by.is_a?(Fixnum) self.approved_by_id = approved_by else self.approved_by = approved_by end self.approved_at = Time.now if save and send_mail Jobs.enqueue(:user_email, type: :signup_after_approval, user_id: id, email_token: email_tokens.first.token ) # ... end end end EXAMPLE OF STATIC ANALYSIS Use `&&` instead of `and`. [rubocop] Method has too many lines. [14/10] [rubocop] Inconsistent indentation detected. [rubocop] Surrounding space missing in default value assignment. [rubocop] Space missing after comma. [rubocop] Align `)` with `(`. [rubocop] Align the parameters of a method call if they span more than one line. [rubocop] discourse/discourse - app/models/user.rb Unused method argument - `apprved_by` [rubocop]
styleguide within your organization ▸ Setup a process for updating your styleguide ▸ Use static analysis tools to auto enforce styleguide violations ▸ Share default configuration for static analysis tools pep8 SCSS Lint Static analysis tools that check for style violations:
Rails code ▸ Use bundler-audit and NSP to detect vulnerable dependencies Static analysis tools that check for security vulnerabilities: brakeman bundler-audit node security project
PHP Mess Detector Radon Reek ShellCheck HLint ▸ Detect complexity and duplication ▸ Detect language or framework-specific smells DETECT COMPLEXITY, DUPLICATION, AND OTHER CODE SMELLS
This cop checks for the use of the send method. class Send < Cop MSG = 'Prefer `Object#__send__` or `Object#public_send` to ' \ '`send`.'.freeze def on_send(node) _receiver, method_name, *args = *node return unless method_name == :send && !args.empty? add_offense(node, :selector) end end end end end
team and community code style guidelines ▸ Detect security vulnerabilities in your code and dependencies ▸ Detect complexity, duplication, and other code smells ▸ Run static analysis locally or during CI ▸ Write your own tool extensions or Code Climate engines