class ActionBlocks::SelectionBuilder

Attributes

base_model[RW]
match_conditions_source[RW]

Public Instance Methods

after_build(parent, *args) click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 119
def after_build(parent, *args)
  if @match_conditions.length == 0
    build_match_conditions_by_reflecting
  else
    @match_conditions_source = "user defined"
  end
end
before_build(parent, id, *args) click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 108
def before_build(parent, id, *args)
  @base_model = parent
  @id = id # aka name
  if args[0]
    @related_model_id = args[0].to_sym
  else
    @related_model_id = @id.to_s.singularize.to_sym
  end
  @related_model_key = "model-#{@related_model_id}"
end
build_match_conditions_by_reflecting() click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 127
def build_match_conditions_by_reflecting
  relation = @base_model.active_model.reflections[@id.to_s]
  if relation && relation.collection?
    @match_conditions_source = "relation"
    if relation.class == ActiveRecord::Reflection::HasManyReflection
      @dsl_delegate.match_condition relation.join_keys.foreign_key.to_sym, :eq, relation.join_keys.key.to_sym
    else
      raise "Relation #{relation.class} not supported"
    end
  end
end
hashify(user) click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 151
def hashify(user)
  {
    key: key,
    type: type
  }
end
key() click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 104
def key
  "selection-#{@base_model.id}-#{@id}"
end
match_conditions_come_from_relation_if_relation_exists() click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 93
def match_conditions_come_from_relation_if_relation_exists
  if @match_conditions_source == 'user defined'
    # puts @base_model.active_model.reflections.keys.inspect
    relation = @base_model.active_model.reflections[@id.to_s]

    if relation && relation.collection?
      errors.add(:match_conditions, "Selections name matches name of has_many, but defines match_conditions anyway")
    end
  end
end
match_conditions_exist() click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 86
def match_conditions_exist
  if @match_conditions.length == 0
    errors.add(:match_conditions, "No match conditions on selection #{key}")
  end
end
match_reqs(*p) click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 139
def match_reqs(*p)
  user = p.first
  match_conditions.map(&:match_reqs)
end
record_filter_reqs(record:, user:) click to toggle source
# File lib/action_blocks/builders/model_builder.rb, line 144
def record_filter_reqs(record:, user:)
  if record.class != @base_model.active_model
    raise "Selection issue."
  end
  @base_model.selection_filter_reqs(record_id: record.id, user: user)
end