Consul 0.4.0 comes with some new features.
Dependencies
- Consul no longer requires
assignable_values, it's optional for when you want to use theauthorize_values_formacro. - Consul no longer uses
ActiveSupport::Memoizablebecause that's deprecated in newer Railses. Consul now uses Memoizer for this.
Temporarily change the current power
When you set Power.current to a power in an RSpec example, you must remember to nilify it afterwards. Otherwise other examples will see your global changes.
A better way is to use the .with_power method to change the current power for the duration of a block:
admin = User.new(:role => 'admin')
admin_power = Power.new(admin)
Power.with_power(admin_power) do
# run code that uses Power.current
end
Power.current will be nil (or its former value) after the block has ended.
A nice shortcut is that when you call with_power with an argument that is not already a Power, Consul will instantiate a Power for you:
admin = User.new(:role => 'admin')
Power.with_power(admin) do
# run code that uses Power.current
end
Support for Ruby collections as powers
Powers can now be vanilla Ruby collections such as arrays or sets:
class Power
...
power :key_figures do
[:amount, :working_costs, :shared_costs] if admin?
end
end
Power.current.key_figures # => [:amount, :working_costs, :shared_costs] for admins
Power.current.key_figures? # => true for admins
Power.current.key_figure?(:amount) # => true for admins
Power.current.key_figure?(:xyz) # => false for everyone
Power.current.key_figures! # does nothing for admins, raises Consul::Powerless otherwise
Power.current.key_figure!(:amount) # does nothing for admins, raises Consul::Powerless otherwise
That means you can also write your assignable_values Show archive.org snapshot value sources like this:
power :key_figures
[:amount, :working_costs, :shared_costs] if admin?
end
(But you can still use def)
Support for arbitrary Ruby objects as powers
Any Ruby object can now be a power:
class Power
...
power :api_key do
'secret-123' if admin?
end
end
Power.current.api_key # => 'secret-123' for admins
Power.current.api_key? # => true for admins
Power.current.api_key! # does nothing for admins, raises Consul::Powerless otherwise