Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cache_accesor for organization permissions related methods #178

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def age
end

def current_organic_context
Organization.current? ? Organization.current : main_organization
Organization.safe_current || main_organization
end

def current_immersive_context_at(path_item)
Expand Down
7 changes: 5 additions & 2 deletions lib/mumuki/domain/helpers/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ def to_s

## Accessible organizations

revamp_accessor :any_granted_organizations, :student_granted_organizations do |_, _, result|
result.map { |org| Mumukit::Platform::Organization.find_by_name!(org) rescue nil }.compact
revamp :any_granted_organizations, :student_granted_organizations, selector_transformer: proc { |it| "@__#{it}__".to_sym } do |attr_name, this, hyper|
this.instance_variable_get(attr_name) ||
hyper.call.map { |org| Mumukit::Platform::Organization.find_by_name!(org) rescue nil }.compact.tap do |organizations|
this.instance_variable_set(attr_name, organizations)
end
end

def has_student_granted_organizations?
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/user_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,26 @@
before { organization.settings.immersive = true }
end
end

context 'is memoized properly' do
let(:user) { create(:user) }

before { organization.switch! }
before { user.make_student_of!(organization); user.save! }
before { expect(Mumukit::Platform.organization_class).to receive(:find_by_name!).with('foo').exactly(1).times.and_return(organization) }

it { expect(user.student_granted_organizations.size).to eq 1 }
it { expect(user.student_granted_organizations).to eq [organization] }
end

context 'memoization may cause issues with uncontextualized users' do
let(:user) { create(:user) }

before { user.make_student_of!(organization); user.save! }
before { expect(Mumukit::Platform.organization_class).to receive(:find_by_name!).with('foo').exactly(1).times.and_return(organization) }

it { expect(user.student_granted_organizations.size).to eq 1 }
it { expect(user.student_granted_organizations).to eq [organization] }
end
end
end