Skip to content

Instantly share code, notes, and snippets.

@clyfe
Created March 8, 2011 11:07
Show Gist options
  • Save clyfe/860150 to your computer and use it in GitHub Desktop.
Save clyfe/860150 to your computer and use it in GitHub Desktop.
CanCan with column
# is usually called with :crud_type and :column, or :action
# {:crud_type=>:update, :column=>"some_colum_name"}
# {:action=>"edit"}
# to allow access cancan must allow both :crud_type and :action
# if cancan says "no", it delegates to default AS behavior
def authorized_for_with_cancan?(options = {})
raise InvalidArgument if options[:crud_type].blank? and options[:action].blank?
if current_ability.present?
crud_type_result = options[:crud_type].nil? ? true : current_ability.can?(options[:crud_type], self)
action_result = options[:action].nil? ? true : current_ability.can?(options[:action].to_sym, self)
column_result = if (options[:crud_type].present && options[:column].present)
current_ability.can?("#{options[:crud_type]}_#{options[:column]}".to_sym, self)
else
true
end
else
crud_type_result, action_result, column_result = false, false, false
end
default_result = authorized_for_without_cancan?(options)
result = (crud_type_result && action_result && column_result) || default_result
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment