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

Updated . Posted . Visible to the public.

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

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
Profile picture of Henning Koch
Henning Koch
Last edit
Felix Eschey
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-01-29 14:07)