Read more

Consul: Dynamically access and query powers for a given name, model class or record

Henning Koch
January 29, 2013Software engineer at makandra GmbH

Consul Show archive.org snapshot 0.6.1+ gives you a way to dynamically access and query powers for a given name, model class or record.
A common use case for this are generic helper methods, e.g. a method to display an "edit" link for any given record
if the user is authorized to change that record:

module CrudHelper

  def edit_record_action(record)
    if current_power.include_record?(:updatable, record)
      link_to 'Edit', [:edit, record]
    end
  end

end
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

You can find a full list of available dynamic calls below:

Dynamic call Equivalent
Power.current.send(:notes) Power.current.notes
Power.current.include?(:notes) Power.current.notes?
Power.current.include!(:notes) Power.current.notes!
Power.current.include?(:notes, Note.last) Power.current.note?(Note.last)
Power.current.include!(:notes, Note.last) Power.current.note!(Note.last)
Power.current.for_record(Note.last) Power.current.notes
Power.current.for_record(:updatable, Note.last) Power.current.updatable_notes
Power.current.for_model(Note) Power.current.notes
Power.current.for_model(:updatable, Note) Power.current.updatable_notes
Power.current.include_model?(Note) Power.current.notes?
Power.current.include_model?(:updatable, Note) Power.current.updatable_notes?
Power.current.include_model!(Note) Power.current.notes!
Power.current.include_model!(:updatable, Note) Power.current.updatable_notes!
Power.current.include_record?(Note.last) Power.current.note?(Note.last)
Power.current.include_record?(:updatable, Note.last) Power.current.updatable_note?(Note.last)
Power.current.include_record!(Note.last) Power.current.note!(Note.last)
Power.current.include_record!(:updatable, Note.last) Power.current.updatable_note!(Note.last)
Power.current.name_for_model(Note) :notes
Power.current.name_for_model(:updatable, Note) :updatable_notes
Power.current.name_for_record(Note.last) :notes
Power.current.name_for_record(:updatable, Note.last) :updatable_notes
Henning Koch
January 29, 2013Software engineer at makandra GmbH
Posted by Henning Koch to makandra dev (2013-01-29 15:07)