Sha256: 82daa17412d97b9a80efdec80a28a8477ca161bba28f2a037cb4970d848b2ba2

Contents?: true

Size: 1.69 KB

Versions: 73

Compression:

Stored size: 1.69 KB

Contents

class RailsOps::Operation::Model::Load < RailsOps::Operation::Model
  class_attribute :_lock_model_at_build
  class_attribute :_load_model_authorization_action

  policy :on_init do
    model_authorization
  end

  # Gets or sets the action verb used for authorizing models on load.
  def self.load_model_authorization_action(*action)
    if action.size == 1
      self._load_model_authorization_action = action.first
    elsif action.size > 1
      fail ArgumentError, 'Too many arguments'
    end

    return _load_model_authorization_action
  end

  def model_authorization
    return unless authorization_enabled?

    unless load_model_authorization_action.nil?
      authorize_model! load_model_authorization_action, model
    end
  end

  def load_model_authorization_action
    self.class.load_model_authorization_action
  end

  load_model_authorization_action :read

  def self.lock_model_at_build(enabled = true)
    self._lock_model_at_build = enabled
  end

  def self.lock_model_at_build?
    _lock_model_at_build.nil? ? RailsOps.config.lock_models_at_build? : _lock_model_at_build
  end

  def model_id_field
    :id
  end

  def find_model
    unless params[model_id_field]
      fail "Param #{model_id_field.inspect} must be given."
    end

    # Get model class
    relation = self.class.model

    # Express intention to lock if required
    relation = relation.lock if self.class.lock_model_at_build?

    # Fetch (and possibly lock) model
    return relation.find_by!(model_id_field => params[model_id_field])
  end

  def build_model
    @model = find_model

    if @model.respond_to?(:parent_op=)
      @model.parent_op = self
    end
  end

  def extract_id_from_params
    params[model_id_field]
  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
rails_ops-1.0.0.beta13 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta12 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta11 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta10 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta9 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta8 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta7 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta6 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta5 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta4 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta3 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta2 lib/rails_ops/operation/model/load.rb
rails_ops-1.0.0.beta1 lib/rails_ops/operation/model/load.rb