Module: DirModel::Model::Relations

Extended by:
ActiveSupport::Concern
Defined in:
lib/dir_model/model/relations.rb

Instance Method Summary collapse

Instance Method Details

#append_dir_model(source_path, options = {}) ⇒ Model

Appends model to the parent and returns it

Returns:

  • (Model)

    return the child if it is valid, otherwise returns nil



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dir_model/model/relations.rb', line 22

def append_dir_model(source_path, options={})
  relation_name = options[:relation_name]
  _options      = options[:relation_options]
  related_class = _options[:dir_model_class]
  foreign_key   = _options[:foreign_key]
  foreign_value = self.send(foreign_key)

  related_dir_model = related_class.new(source_path,
    options.reverse_merge(parent: self, foreign_value: foreign_value))

  return unless related_dir_model

  unless related_dir_model.skip?
    if public_send(relation_name).present?
      raise StandardError.new("There are more of one #{relation_name} => #{related_class} for #{self.class.name}")
    end
    public_send("#{relation_name}=", related_dir_model)
    related_dir_model
  else
    nil
  end
end

#append_dir_models(source_path, options = {}) ⇒ Model

Appends modeld to the parent and returns it

Returns:

  • (Model)

    return the child if it is valid, otherwise returns nil



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dir_model/model/relations.rb', line 48

def append_dir_models(source_path, options={})
  relation_name = self.class.has_many_relationship.keys.first
  _options      = self.class.has_many_relationship.values.first
  related_class = _options[:dir_model_class]
  foreign_key   = _options[:foreign_key]
  foreign_value = self.send(foreign_key)

  related_dir_model = related_class.new(source_path,
    options.reverse_merge(parent: self, foreign_value: foreign_value))

  return unless related_dir_model

  unless related_dir_model.skip?
    public_send(relation_name) << related_dir_model
    related_dir_model
  else
    nil
  end
end

#child?Boolean

Returns true, if the instance is a child

Returns:

  • (Boolean)

    returns true, if the instance is a child



11
12
13
# File 'lib/dir_model/model/relations.rb', line 11

def child?
  !!parent
end

#has_many?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/dir_model/model/relations.rb', line 72

def has_many?
  false
end

#has_one?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/dir_model/model/relations.rb', line 68

def has_one?
  false
end

#has_relations?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/dir_model/model/relations.rb', line 15

def has_relations?
  has_one? || has_many?
end