Module: DSNode::DSResource::ClassMethods

Defined in:
lib/ds_node/ds_resource.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_many_ds_resources(name, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ds_node/ds_resource.rb', line 109

def belongs_to_many_ds_resources name, options = {}
  single_name = options.key?(:single_name) ? options.delete(:single_name) : name.to_s.singularize.to_sym
  prevent_destroy_last = options.key?(:prevent_destroy_last) ? options.delete(:prevent_destroy_last) : false

  belongs_to_many name, **options.reverse_merge({
    class_name: "DSNode::Resource",
  })

  attr_accessor :"new_#{single_name}_files"
  attr_accessor :"destroy_#{single_name}_ids"
  attr_accessor :"remove_#{single_name}_ids"

  before_save do
    if send(:"new_#{single_name}_files").present?
      new_resources = Array(send(:"new_#{single_name}_files")).map do |file|
        DSNode::Resource.create! file: file
      end
      send :"#{name}=", send(name) + new_resources
      send :"new_#{single_name}_files=", nil
    end
  end

  before_save do
    if send(:"destroy_#{single_name}_ids").present?
      (send(:"destroy_#{single_name}_ids") || []).map(&:to_i).each do |id|
        send :"#{single_name}_ids=", send(:"#{single_name}_ids") - [id]
        raise PreventDestroyLast if prevent_destroy_last && send(name).count == 1
        DSNode::Resource.destroy(id)
      end
      send :"destroy_#{single_name}_ids=", nil
    end
  end

  before_save do
    if send(:"remove_#{single_name}_ids").present?
      (send(:"remove_#{single_name}_ids") || []).map(&:to_i).each do |id|
        send :"#{single_name}_ids=", send(:"#{single_name}_ids") - [id]
        raise PreventDestroyLast if prevent_destroy_last && send(name).count == 0
      end
      send :"remove_#{single_name}_ids=", nil
    end
  end
end

#ds_resource(name, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ds_node/ds_resource.rb', line 30

def ds_resource name, options = {}
  should_validate = options.fetch(:validate, true)

  belongs_to name, **options.reverse_merge({
    class_name: "DSNode::Resource",
    required: false,
  })

  inquirer_accessor = :"#{name}?"
  define_method inquirer_accessor do
    send(name).present?
  end

  writer_accessor = :"#{name}_file"
  attr_accessor writer_accessor
  validates writer_accessor, recognized_file_type: true if should_validate

  destroy_accessor = :"destroy_#{name}"
  attr_accessor destroy_accessor

  remove_accessor = :"remove_#{name}"
  attr_accessor remove_accessor

  after_save do
    if file = send(writer_accessor)
      send :"build_#{name}", file: file
      send :"#{writer_accessor}=", nil
      save
    end
  end

  after_save do
    if send(destroy_accessor).to_i == 1
      send(name).try(:destroy)
      send :"#{name}=", nil
      send :"#{destroy_accessor}=", nil
      save
    end
  end

  after_save do
    if send(remove_accessor).to_i == 1
      send :"#{name}=", nil
      send :"#{remove_accessor}=", nil
      save
    end
  end
end

#has_many_ds_resources(name, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ds_node/ds_resource.rb', line 79

def has_many_ds_resources name, options = {}
  single_name = options.key?(:single_name) ? options.delete(:single_name) : name.to_s.singularize.to_sym
  prevent_destroy_last = options.key?(:prevent_destroy_last) ? options.delete(:prevent_destroy_last) : false

  has_many name, options

  validates :"new_#{single_name}_files", recognized_file_type: true

  attr_accessor :"new_#{single_name}_files", :"destroy_#{single_name}_ids"

  after_save do
    if send(:"new_#{single_name}_files").present?
      Array(send(:"new_#{single_name}_files")).each do |file|
        send(name).create! file: file
      end
      send :"new_#{single_name}_files=", nil
    end
  end

  after_save do
    if send(:"destroy_#{single_name}_ids").present?
      (send(:"destroy_#{single_name}_ids") || []).each do |id|
        raise PreventDestroyLast if prevent_destroy_last && send(name).count == 1
        send(name).destroy(id)
      end
      send :"destroy_#{single_name}_ids=", nil
    end
  end
end