Created
April 13, 2023 08:26
-
-
Save verlihirsh/8c8ab840a8422da971dbd3aa468a65fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ApplicationRecord | |
| has_many :posts | |
| # Violates Single Responsibility Principle (SRP) | |
| def make_admin | |
| self.update(:is_admin, true) | |
| end | |
| # Violates Law of Demeter (LoD) and N+1 query issue | |
| def recent_posts_with_comments | |
| self.posts.includes(:comments).where('created_at > ?', 1.week.ago).order('created_at DESC').each do |post| | |
| puts "Post: #{post.title}" | |
| post.comments.each { |comment| puts "Comment: #{comment.body}" } | |
| end | |
| end | |
| # Violates Law of Demeter (LoD) | |
| def latest_post_title | |
| self.posts.last.title | |
| end | |
| # Violates KISS (Keep It Simple, Stupid) Principle | |
| def status_summary | |
| if posts.count > 10 | |
| "#{name} is an active user with #{posts.count} posts and their latest post is '#{posts.last.title}'" | |
| elsif posts.count > 0 | |
| "#{name} has #{posts.count} posts and their latest post is '#{posts.last.title}'" | |
| else | |
| "#{name} hasn't written any posts yet." | |
| end | |
| end | |
| # Violates Single Responsibility Principle (SRP) | |
| def generate_report(format) | |
| case format | |
| when :csv | |
| CSV.generate(headers: true) do |csv| | |
| csv << ['Name', 'Email', 'Is Admin', 'Posts Count'] | |
| csv << [name, email, is_admin, posts.count] | |
| end | |
| when :json | |
| { | |
| name: name, | |
| email: email, | |
| is_admin: is_admin, | |
| posts_count: posts.count | |
| }.to_json | |
| else | |
| "Unsupported format" | |
| end | |
| end | |
| # Violates Dependency Inversion Principle (DIP) | |
| def self.from_omniauth(auth) | |
| user = User.find_or_initialize_by(provider: auth.provider, uid: auth.uid) | |
| user.name = auth.info.name | |
| user.email = auth.info.email | |
| user.password = Devise.friendly_token[0, 20] | |
| user.save | |
| user | |
| end | |
| # ... (the rest of the file) | |
| end | |
| class UsersController < ApplicationController | |
| # ... | |
| # Violates DRY (Don't Repeat Yourself) Principle | |
| def update | |
| user = User.find(params[:id]) | |
| if user.update(user_params) | |
| flash[:notice] = "User updated successfully!" | |
| redirect_to user_path(user) | |
| else | |
| flash[:alert] = "User update failed!" | |
| render :edit | |
| end | |
| end | |
| # Violates Encapsulation Principle | |
| def user_params | |
| params.require(:user).permit(:name, :email, :is_admin) | |
| end | |
| # Violates Law of Demeter (LoD) | |
| def show | |
| @user = User.find(params[:id]) | |
| @latest_post_title = @user.posts.last.title | |
| end | |
| # Violates N+1 query issue | |
| def index | |
| @users = User.all.includes(:posts) | |
| @users.each do |user| | |
| puts "User: #{user.name}" | |
| user.posts.each { |post| puts "Post: #{post.title}" } | |
| end | |
| end | |
| # Violates SQL injection vulnerability | |
| def search | |
| search_term = params[:search_term] | |
| @users = User.where("name LIKE '#{search_term}%'") | |
| end | |
| # Violates Fat Controller Principle | |
| def promote_to_admin | |
| user = User.find(params[:id]) | |
| user.is_admin = true | |
| user.save | |
| redirect_to users_path, notice: "#{user.name} is now an admin." | |
| end | |
| # Violates YAGNI (You Aren't Gonna Need It) Principle | |
| def deactivate_user | |
| user = User.find(params[:id]) | |
| user.update_attribute(:active, false) | |
| flash[:notice] = "User deactivated." | |
| redirect_to users_path | |
| end | |
| # ... (the rest of the file) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment