Class: ForestAdminDatasourceRpc::Collection

Inherits:
ForestAdminDatasourceToolkit::Collection
  • Object
show all
Includes:
ForestAdminDatasourceCustomizer::Decorators::Action, Utils, ForestAdminDatasourceToolkit::Components
Defined in:
lib/forest_admin_datasource_rpc/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(datasource, name, options, schema) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 9

def initialize(datasource, name, options, schema)
  super(datasource, name)
  @options = options
  @client = RpcClient.new(@options[:uri], ForestAdminRpcAgent::Facades::Container.cache(:auth_secret))
  @rpc_collection_uri = "/forest/rpc/#{name}"
  @base_params = { collection_name: name }

  ForestAdminRpcAgent::Facades::Container.logger.log('Debug', "Create Rpc collection #{name}.")

  enable_count if schema[:countable]
  enable_search if schema[:searchable]
  add_fields(schema[:fields])
  add_segments(schema[:segments])
  schema[:charts].each { |chart| add_chart(chart) }
  schema[:actions].each do |action_name, action_schema|
    add_action(action_name.to_s, BaseAction.from_plain_object(action_schema))
  end
end

Instance Method Details

#add_fields(fields) ⇒ Object



28
29
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
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 28

def add_fields(fields)
  fields.each do |field_name, schema|
    field_name = field_name.to_s
    type = schema[:type]
    schema.delete(:type)
    case type
    when 'Column'
      add_field(field_name, ForestAdminDatasourceToolkit::Schema::ColumnSchema.new(**schema))
    when 'ManyToMany'
      add_field(field_name, ForestAdminDatasourceToolkit::Schema::Relations::ManyToManySchema.new(**schema))
    when 'OneToMany'
      add_field(field_name, ForestAdminDatasourceToolkit::Schema::Relations::OneToManySchema.new(**schema))
    when 'ManyToOne'
      add_field(field_name, ForestAdminDatasourceToolkit::Schema::Relations::ManyToOneSchema.new(**schema))
    when 'OneToOne'
      add_field(field_name, ForestAdminDatasourceToolkit::Schema::Relations::OneToOneSchema.new(**schema))
    when 'PolymorphicManyToOne'
      add_field(field_name,
                ForestAdminDatasourceToolkit::Schema::Relations::PolymorphicManyToOneSchema.new(**schema))
    when 'PolymorphicOneToMany'
      add_field(field_name,
                ForestAdminDatasourceToolkit::Schema::Relations::PolymorphicOneToManySchema.new(**schema))
    when 'PolymorphicOneToOne'
      add_field(field_name,
                ForestAdminDatasourceToolkit::Schema::Relations::PolymorphicOneToOneSchema.new(**schema))
    end
  end
end

#aggregate(caller, filter, aggregation, limit = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 106

def aggregate(caller, filter, aggregation, limit = nil)
  params = build_params(caller: caller.to_h, filter: filter.to_h, aggregation: aggregation.to_h, limit: limit,
                        timezone: caller.timezone)
  url = "#{@rpc_collection_uri}/aggregate"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' aggregate call to the Rpc agent on #{url}."
  )

  @client.call_rpc(url, method: :post, payload: params)
end

#create(caller, data) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 70

def create(caller, data)
  params = build_params(caller: caller.to_h, timezone: caller.timezone, data: data)
  url = "#{@rpc_collection_uri}/create"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' creation call to the Rpc agent on #{url}."
  )

  @client.call_rpc(url, method: :post, payload: params)
end

#delete(caller, filter) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 94

def delete(caller, filter)
  params = build_params(caller: caller.to_h, filter: filter.to_h, timezone: caller.timezone)
  url = "#{@rpc_collection_uri}/delete"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' deletion call to the Rpc agent on #{url}."
  )

  @client.call_rpc(url, method: :post, payload: params)
end

#execute(caller, name, data, filter = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 119

def execute(caller, name, data, filter = nil)
  data = encode_form_data(data)
  params = build_params(
    caller: caller.to_h,
    timezone: caller.timezone,
    action: name,
    filter: filter&.to_h,
    data: data
  )
  url = "#{@rpc_collection_uri}/action-execute"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' action #{name} call to the Rpc agent on #{url}."
  )

  @client.call_rpc(url, method: :post, payload: params)
end

#get_form(caller, name, data = nil, filter = nil, metas = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 138

def get_form(caller, name, data = nil, filter = nil, metas = nil)
  params = build_params(action: name)
  if caller
    data = encode_form_data(data)
    params = params.merge({ caller: caller.to_h, timezone: caller.timezone, filter: filter&.to_h, metas: metas,
                            data: data })
  end
  url = "#{@rpc_collection_uri}/action-form"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' action form #{name} call to the Rpc agent on #{url}."
  )

  result = @client.call_rpc(url, method: :post, payload: params, symbolize_keys: true)
  FormFactory.build_elements(result).map do |field|
    Actions::ActionFieldFactory.build(field.to_h)
  end
end

#list(caller, filter, projection) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 57

def list(caller, filter, projection)
  params = build_params(caller: caller.to_h, filter: filter.to_h, projection: projection,
                        timezone: caller.timezone)
  url = "#{@rpc_collection_uri}/list"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' list call to the Rpc agent on #{url}."
  )

  @client.call_rpc(url, method: :post, payload: params)
end

#render_chart(caller, name, record_id) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 158

def render_chart(caller, name, record_id)
  params = build_params(caller: caller.to_h, name: name, record_id: record_id)
  url = "#{@rpc_collection_uri}/chart"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' chart #{name} call to the Rpc agent on #{url}."
  )

  @client.call_rpc(url, method: :post, payload: params)
end

#update(caller, filter, data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/forest_admin_datasource_rpc/collection.rb', line 82

def update(caller, filter, data)
  params = build_params(caller: caller.to_h, filter: filter.to_h, data: data, timezone: caller.timezone)
  url = "#{@rpc_collection_uri}/update"

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding '#{@name}' update call to the Rpc agent on #{url}."
  )

  @client.call_rpc(url, method: :post, payload: params)
end