lib/heimdallr/proxy/record.rb in heimdallr-1.0.0.RC2 vs lib/heimdallr/proxy/record.rb in heimdallr-1.0.0

- old
+ new

@@ -16,11 +16,11 @@ # @param object proxified record # @option options [Boolean] implicit proxy type def initialize(context, record, options={}) @context, @record, @options = context, record, options - @restrictions = @record.class.restrictions(context) + @restrictions = @record.class.restrictions(context, record) end # @method decrement(field, by=1) # @macro [new] delegate # Delegates to the corresponding method of underlying object. @@ -103,12 +103,14 @@ [:delete, :destroy].each do |method| class_eval(<<-EOM, __FILE__, __LINE__) def #{method} scope = @restrictions.request_scope(:delete) - if scope.where({ @record.class.primary_key => @record.to_key }).count != 0 + if scope.where({ @record.class.primary_key => @record.to_key }).any? @record.#{method} + else + raise PermissionError, "Deletion is forbidden" end end EOM end @@ -126,10 +128,14 @@ # @method assign_attributes # @macro delegate delegate :assign_attributes, :to => :@record + # @method attributes= + # @macro delegate + delegate :attributes=, :to => :@record + # Class name of the underlying model. # @return [String] def class_name @record.class.name end @@ -242,9 +248,18 @@ model: @record.class, context: @context, record: @record, options: @options }.merge(@restrictions.reflection) + end + + def modifiable? + @restrictions.can? :update + end + + def destroyable? + scope = @restrictions.request_scope(:delete) + scope.where({ @record.class.primary_key => @record.to_key }).any? end protected # Raises an exception if any of the changed attributes are not valid \ No newline at end of file