Module: RestRails::ApplicationHelper

Included in:
DataController
Defined in:
app/helpers/rest_rails/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#attachment_hash(attached) ⇒ Object



24
25
26
# File 'app/helpers/rest_rails/application_helper.rb', line 24

def attachment_hash(attached)
  {attachment_id: attached.id, url: blob_link(attached)}
end

#attachments_for(ar_object) ⇒ Object



54
55
56
57
58
# File 'app/helpers/rest_rails/application_helper.rb', line 54

def attachments_for(ar_object)
  meths = ar_object.class.methods.map(&:to_s)
  attach_meths = meths.select{|x| x.include?("with_attached")}
  attach_meths.map{|x| x[14..-1].to_sym}
end


28
29
30
31
32
33
34
35
# File 'app/helpers/rest_rails/application_helper.rb', line 28

def blob_link(x)
  if Rails.env == "production"
    host = RestRails.production_domain || ""
  else
    host = RestRails.development_domain || "http://localhost:3000"
  end
  Rails.application.routes.url_helpers.rails_blob_url(x, host: host)
end

#columns_for(ar_object) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/rest_rails/application_helper.rb', line 60

def columns_for(ar_object)
  table = ar_object.class.table_name.to_sym
  cols  = ar_object.class.column_names.map(&:to_sym)
  cols += attachments_for(ar_object) if RestRails.active_storage_attachments
  cols += nested_attributes_for(ar_object) unless ar_object.class.nested_attributes_options.blank?

  return cols if RestRails.permit == :all || RestRails.permit[table] == :all

  cols.select{|x| RestRails.permit[table].include?(x) }
end

#model_for(table_name) ⇒ Object

OTHER HELPERS


89
90
91
92
93
94
95
96
97
# File 'app/helpers/rest_rails/application_helper.rb', line 89

def model_for(table_name)
  Zeitwerk::Loader.eager_load_all
  tables = ActiveRecord::Base.descendants.reject(&:abstract_class).index_by(&:table_name)

  raise  RestRails::Error.new "Table '#{table_name}' does not exist in your database!" unless tables.keys.include?(table_name)

  # Take the tablename, and make the Model of the relative table_name
  tables[table_name]
end

#nested_attributes_for(ar_object) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/rest_rails/application_helper.rb', line 71

def nested_attributes_for(ar_object)
  ar_model = ar_object.class
  tables = ar_model.nested_attributes_options.keys

  tables.map do |x|
    nested_model = ar_model.reflect_on_association(x).klass
    cols = nested_model.column_names.map(&:to_sym)
    key = "#{x}_attributes".to_sym
    cols << :_destroy

    {key => cols}
  end
end

#prepare_attachment(attached) ⇒ Object



45
46
47
48
49
50
51
52
# File 'app/helpers/rest_rails/application_helper.rb', line 45

def prepare_attachment(attached)
  if attached.class == ActiveStorage::Attached::Many
    return attached.map{|x| attachment_hash(x) }
  elsif attached.class == ActiveStorage::Attached::One
    x = attached.attachment
    return attachment_hash(x) unless x.nil?
  end
end

#prepare_column(col_value) ⇒ Object



37
38
39
40
41
42
43
# File 'app/helpers/rest_rails/application_helper.rb', line 37

def prepare_column(col_value)
  if [ActiveStorage::Attached::Many, ActiveStorage::Attached::One].include?(col_value.class)
    return prepare_attachment(col_value)
  else
    return col_value
  end
end

#serial_attachments(ar_object) ⇒ Object

SERIALIZE ATTACHMENTS FOR JSON


13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/rest_rails/application_helper.rb', line 13

def serial_attachments(ar_object)
  h = {}
  attachment_types = attachments_for(ar_object)
  attachment_types.each do |att|
    attached = ar_object.public_send(att)
    next if attached.nil?
    h[att]   = prepare_attachment(attached)
  end
  return h
end

#standardize_json(ar_object) ⇒ Object



3
4
5
6
7
# File 'app/helpers/rest_rails/application_helper.rb', line 3

def standardize_json(ar_object)
  h = ar_object.serializable_hash
  h.merge!(serial_attachments(ar_object)) if attachments_for(ar_object).present?
  h
end

#verify_permitted!(table) ⇒ Object



99
100
101
102
103
104
105
# File 'app/helpers/rest_rails/application_helper.rb', line 99

def verify_permitted!(table)
  if [:none, nil].include?(RestRails.permit[table])
    raise RestRails::Error.new "NOT PERMITTED. Must add table to whitelisted params."
  elsif !RestRails.permit.is_a?(Hash) && (RestRails.permit != :all)
    raise RestRails::Error.new "RestRails initializer permit is not valid!"
  end
end