Class: ActionBlocks::SelectionBuilder

Inherits:
BlockType show all
Defined in:
lib/action_blocks/builders/model_builder.rb

Instance Attribute Summary collapse

Attributes inherited from BaseBuilder

#dsl_delegate, #id

Instance Method Summary collapse

Methods inherited from BlockType

#is_block?, #type

Methods inherited from BaseBuilder

#after_load, array_fields, block_type, builds, builds_many, delegate_class, #evaluate, #freeze, includes_scheme_helpers, #initialize, #is_block?, references, sets, sets_many, #ui_reference, #valid?

Constructor Details

This class inherits a constructor from ActionBlocks::BaseBuilder

Instance Attribute Details

#base_modelObject

Returns the value of attribute base_model.



79
80
81
# File 'lib/action_blocks/builders/model_builder.rb', line 79

def base_model
  @base_model
end

#match_conditions_sourceObject

Returns the value of attribute match_conditions_source.



79
80
81
# File 'lib/action_blocks/builders/model_builder.rb', line 79

def match_conditions_source
  @match_conditions_source
end

Returns the value of attribute related_model_id.



79
80
81
# File 'lib/action_blocks/builders/model_builder.rb', line 79

def related_model_id
  @related_model_id
end

Instance Method Details

#after_build(parent, *args) ⇒ Object



119
120
121
122
123
124
125
# 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) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# 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_reflectingObject



127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ Object



151
152
153
154
155
156
# File 'lib/action_blocks/builders/model_builder.rb', line 151

def hashify(user)
  {
    key: key,
    type: type
  }
end

#keyObject



104
105
106
# 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_existsObject



93
94
95
96
97
98
99
100
101
102
# 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_existObject



86
87
88
89
90
# 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) ⇒ Object



139
140
141
142
# 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:) ⇒ Object



144
145
146
147
148
149
# 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