I have the following complex method. I am trying to find and implement possible improvements. Right now, I have translated the last if statement into an 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 also like to add specifications before refactoring. I added the first one. How to look 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
source share