Class: Api::V2::InfoController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/api/v2/info_controller.rb

Instance Method Summary collapse

Instance Method Details

#dslObject

GET ‘/api/v2/info/dsl’



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/api/v2/info_controller.rb', line 54

def dsl
  pivot = {}
  # if Rails.env.development?
  #   Rails.configuration.eager_load_namespaces.each(&:eager_load!) if Rails.version.to_i == 5 #Rails 5
  #   Zeitwerk::Loader.eager_load_all if Rails.version.to_i >= 6 #Rails 6
  # end
  ApplicationRecord.subclasses.each do |d|
    # Only if current user can read the model
    if can? :read, d
      model = d.to_s.underscore.tableize
      pivot[model] = (d.instance_methods(false).include?(:json_attrs) && !d.json_attrs.blank?) ? d.json_attrs : nil
    end
  end
  render json: pivot.to_json, status: 200
end

#rolesObject

api :GET, ‘/api/v2/info/roles’ it returns the roles list



14
15
16
# File 'app/controllers/api/v2/info_controller.rb', line 14

def roles
  render json: ::Role.all.to_json, status: 200
end

#schemaObject

GET ‘/api/v2/info/schema’



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/api/v2/info_controller.rb', line 24

def schema
  pivot = {}
  # if Rails.env.development?
  #   Rails.configuration.eager_load_namespaces.each(&:eager_load!) if Rails.version.to_i == 5 #Rails 5
  #   Zeitwerk::Loader.eager_load_all if Rails.version.to_i >= 6 #Rails 6
  # end
  ApplicationRecord.subclasses.each do |d|
    # Only if current user can read the model
    if can? :read, d
      model = d.to_s.underscore.tableize
      pivot[model] ||= {}
      d.columns_hash.each_pair do |key, val| 
        pivot[model][key] = val.type unless key.ends_with? "_id"
      end
      # Only application record descendants to have a clean schema
      pivot[model][:associations] ||= {
        has_many: d.reflect_on_all_associations(:has_many).map { |a| 
          a.name if (((a.options[:class_name].presence || a.name).to_s.classify.constantize.new.is_a? ApplicationRecord) rescue false)
        }.compact, 
        belongs_to: d.reflect_on_all_associations(:belongs_to).map { |a| 
          a.name if (((a.options[:class_name].presence || a.name).to_s.classify.constantize.new.is_a? ApplicationRecord) rescue false)
        }.compact
      }
      pivot[model][:methods] ||= (d.instance_methods(false).include?(:json_attrs) && !d.json_attrs.blank?) ? d.json_attrs[:methods] : nil
    end
  end
  render json: pivot.to_json, status: 200
end

#translationsObject

GET ‘/api/v2/info/translations’



19
20
21
# File 'app/controllers/api/v2/info_controller.rb', line 19

def translations
  render json: I18n.t(".", locale: (params[:locale].presence || :it)).to_json, status: 200
end

#versionObject

api :GET, ‘/api/v2/info/version’, “Just prints the APPVERSION.”



8
9
10
# File 'app/controllers/api/v2/info_controller.rb', line 8

def version
  render json: { version: ModelDrivenApi::VERSION }.to_json, status: 200
end