Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:59:13+00:00 2026-06-13T13:59:13+00:00

I have the following complex method. I’m trying to find and implement possible improvements.

  • 0

I have the following complex method. I’m trying to find and implement possible improvements. Right now I moved last if statement to Access class.

def add_access(access)
   if access.instance_of?(Access)
     up = UserAccess.find(:first, :conditions => ['user_id = ? AND access_id = ?', self.id, access.id])
     if !up && company
       users = company.users.map{|u| u.id unless u.blank?}.compact
       num_p = UserAccess.count(:conditions => ['user_id IN (?) AND access_id = ?', users, access.id])
       if num_p < access.limit
         UserAccess.create(:user => self, :access => access)
       else
         return "You have exceeded the maximum number of alotted permissions"
       end
     end
   end
 end

I would like to add also specs before refactoring. I added first one. How should looks like others?

  describe "#add_permission" do
    before do
      @permission = create(:permission)
      @user = create(:user)
    end

    it "allow create UserPermission" do
      expect {
        @user.add_permission(@permission)
      }.to change {
        UserPermission.count
      }.by(1)
    end
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:59:15+00:00Added an answer on June 13, 2026 at 1:59 pm

    Here is how I would do it.

    Make the check on the Access more like an initial assertion, and raise an error if that happens.

    Make a new method to check for an existing user access – that seems reusable, and more readable.

    Then, the company limit is more like a validation to me, move this to the UserAccess class as a custom validation.

    class User
    
      has_many :accesses, :class_name=>'UserAccess'
    
      def add_access(access)
        raise "Can only add a Access: #{access.inspect}" unless access.instance_of?(Access)
    
        if has_access?(access)
          logger.debug("User #{self.inspect} already has the access #{access}")
          return false
        end
    
        accesses.create(:access => access)
      end
    
      def has_access?(access)
        accesses.find(:first, :conditions => {:access_id=> access.id})
      end
    
    end
    
    class UserAccess
    
      validate :below_company_limit
    
      def below_company_limit
        return true unless company
        company_user_ids = company.users.map{|u| u.id unless u.blank?}.compact
        access_count = UserAccess.count(:conditions => ['user_id IN (?) AND access_id = ?', company_user_ids, access.id])
        access_count < access.limit
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a complex query with linq. I have a C# method
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
We have the following code working for a complex rails form with checkboxes. I'm
I have a complex html DOM tree of the following nature: <table> ... <tr>
I have the following bison grammar (as part of a more complex grammar): classDeclaration
I have the following code, with which I am trying to create a seperated
My problem is a little bit complex. I have the following code: $(document).ready(function() {
I have the following Action method. public ActionResult Action() { var model = new
I have the following bean method signatures: public String foo(); public List<String> getList(String bar);
I have a complex method I want to test, It recieves user, UserUpdatedDetails(password, restore

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.