avo 0.5.0.beta4 → 0.5.0.beta5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of avo might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +4 -2
- data/Gemfile.lock +1 -1
- data/app/components/avo/index/grid_item_component.html.erb +8 -8
- data/app/components/avo/index/grid_item_component.rb +6 -12
- data/app/components/avo/index/resource_table_component.html.erb +1 -1
- data/app/components/avo/index/table_row_component.html.erb +1 -1
- data/app/controllers/avo/actions_controller.rb +2 -4
- data/app/controllers/avo/application_controller.rb +6 -3
- data/app/controllers/avo/base_controller.rb +0 -3
- data/app/controllers/avo/relations_controller.rb +1 -1
- data/app/controllers/avo/search_controller.rb +1 -1
- data/app/frontend/packs/application.js +1 -1
- data/app/views/avo/base/_actions.html.erb +0 -6
- data/app/views/avo/base/_boolean_filter.html.erb +1 -1
- data/app/views/avo/base/_filters.html.erb +3 -11
- data/app/views/avo/base/_select_filter.html.erb +1 -1
- data/app/views/avo/home/_resources.html.erb +0 -1
- data/app/views/layouts/avo/_filter_wrapper.html.erb +1 -1
- data/app/views/layouts/avo/application.html.erb +5 -0
- data/config/webpacker.yml +10 -23
- data/lib/avo.rb +5 -16
- data/lib/avo/app.rb +190 -0
- data/lib/avo/base_action.rb +115 -0
- data/lib/avo/base_resource.rb +321 -0
- data/lib/avo/configuration.rb +6 -0
- data/lib/avo/engine.rb +12 -31
- data/lib/avo/{app/fields → fields}/badge_field.rb +1 -1
- data/lib/avo/{app/fields/field.rb → fields/base_field.rb} +3 -7
- data/lib/avo/{app/fields/belongs_to.rb → fields/belongs_to_field.rb} +3 -3
- data/lib/avo/{app/fields → fields}/boolean_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/boolean_group_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/code_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/country_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/currency_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/date_field.rb +0 -2
- data/lib/avo/{app/fields → fields}/date_time_field.rb +0 -2
- data/lib/avo/{app/fields → fields}/external_image_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/field_extensions/has_field_name.rb +0 -0
- data/lib/avo/{app/fields → fields}/field_extensions/visible_in_different_views.rb +1 -1
- data/lib/avo/{app/fields → fields}/file_field.rb +1 -2
- data/lib/avo/{app/fields → fields}/files_field.rb +1 -2
- data/lib/avo/{app/fields → fields}/gravatar_field.rb +1 -1
- data/lib/avo/{app/fields/has_and_belongs_to_many.rb → fields/has_and_belongs_to_many_field.rb} +1 -1
- data/lib/avo/{app/fields/has_many.rb → fields/has_many_field.rb} +1 -1
- data/lib/avo/{app/fields/has_one.rb → fields/has_one_field.rb} +1 -1
- data/lib/avo/{app/fields → fields}/heading_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/hidden_field.rb +0 -2
- data/lib/avo/{app/fields → fields}/id_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/key_value_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/markdown_field.rb +1 -3
- data/lib/avo/{app/fields → fields}/number_field.rb +0 -2
- data/lib/avo/{app/fields → fields}/password_field.rb +0 -2
- data/lib/avo/{app/fields → fields}/select_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/status_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/text_field.rb +1 -1
- data/lib/avo/{app/fields → fields}/textarea_field.rb +0 -2
- data/lib/avo/{app/fields → fields}/trix_field.rb +1 -3
- data/lib/avo/filters/base_filter.rb +20 -0
- data/lib/avo/filters/boolean_filter.rb +7 -0
- data/lib/avo/filters/select_filter.rb +7 -0
- data/lib/avo/{app/grid_fields → grid_fields}/body_field.rb +0 -2
- data/lib/avo/{app/grid_fields → grid_fields}/grid_field.rb +0 -0
- data/lib/avo/{app/grid_fields → grid_fields}/preview_field.rb +0 -2
- data/lib/avo/{app/grid_fields → grid_fields}/title_field.rb +0 -2
- data/lib/avo/licensing/community_license.rb +6 -0
- data/lib/avo/licensing/h_q.rb +88 -0
- data/lib/avo/licensing/license.rb +50 -0
- data/lib/avo/licensing/license_manager.rb +22 -0
- data/lib/avo/licensing/null_license.rb +14 -0
- data/lib/avo/licensing/pro_license.rb +11 -0
- data/lib/avo/loaders/actions_loader.rb +6 -0
- data/lib/avo/loaders/fields_loader.rb +27 -0
- data/lib/avo/loaders/filters_loader.rb +6 -0
- data/lib/avo/loaders/loader.rb +15 -0
- data/lib/avo/services/authorization_service.rb +93 -0
- data/lib/avo/services/panel_service.rb +27 -0
- data/lib/avo/{app/tools_manager.rb → tools_manager.rb} +0 -0
- data/lib/avo/version.rb +1 -1
- data/lib/generators/avo/action_generator.rb +11 -5
- data/lib/generators/avo/controller_generator.rb +14 -8
- data/lib/generators/avo/filter_generator.rb +14 -8
- data/lib/generators/avo/install_generator.rb +14 -10
- data/lib/generators/avo/locales_generator.rb +11 -7
- data/lib/generators/avo/partials_generator.rb +11 -5
- data/lib/generators/avo/resource_generator.rb +33 -15
- data/lib/generators/avo/templates/{action.rb → action.tt} +2 -4
- data/lib/generators/avo/templates/filters/boolean_filter.tt +11 -0
- data/lib/generators/avo/templates/filters/select_filter.tt +11 -0
- data/lib/generators/avo/templates/initializer/{avo.rb → avo.tt} +6 -0
- data/lib/generators/avo/templates/resource/controller.tt +2 -0
- data/lib/generators/avo/templates/resource/resource.tt +17 -0
- data/public/avo-packs/css/{application-3598cfbb.css → application-c75ac28c.css} +1 -1
- data/public/avo-packs/css/application-c75ac28c.css.br +0 -0
- data/public/avo-packs/css/{application-3598cfbb.css.gz → application-c75ac28c.css.gz} +0 -0
- data/public/avo-packs/js/{application-6ba2bd1bca05d69b96e0.js → application-8849c6e2c8f75d55c666.js} +3 -3
- data/public/avo-packs/js/{application-6ba2bd1bca05d69b96e0.js.LICENSE.txt → application-8849c6e2c8f75d55c666.js.LICENSE.txt} +0 -0
- data/public/avo-packs/js/application-8849c6e2c8f75d55c666.js.br +0 -0
- data/public/avo-packs/js/application-8849c6e2c8f75d55c666.js.gz +0 -0
- data/public/avo-packs/js/{application-6ba2bd1bca05d69b96e0.js.map → application-8849c6e2c8f75d55c666.js.map} +1 -1
- data/public/avo-packs/js/application-8849c6e2c8f75d55c666.js.map.br +0 -0
- data/public/avo-packs/js/{application-6ba2bd1bca05d69b96e0.js.map.gz → application-8849c6e2c8f75d55c666.js.map.gz} +0 -0
- data/public/avo-packs/manifest.json +6 -6
- data/public/avo-packs/manifest.json.br +0 -0
- data/public/avo-packs/manifest.json.gz +0 -0
- metadata +72 -73
- data/config/webpacker_packed.yml +0 -86
- data/lib/avo/app/action.rb +0 -145
- data/lib/avo/app/actions_loader.rb +0 -11
- data/lib/avo/app/app.rb +0 -183
- data/lib/avo/app/fields_loader.rb +0 -29
- data/lib/avo/app/filter.rb +0 -23
- data/lib/avo/app/filters/boolean_filter.rb +0 -13
- data/lib/avo/app/filters/select_filter.rb +0 -13
- data/lib/avo/app/licensing/community_license.rb +0 -4
- data/lib/avo/app/licensing/hq.rb +0 -86
- data/lib/avo/app/licensing/license.rb +0 -48
- data/lib/avo/app/licensing/license_manager.rb +0 -25
- data/lib/avo/app/licensing/null_license.rb +0 -12
- data/lib/avo/app/licensing/pro_license.rb +0 -9
- data/lib/avo/app/resource.rb +0 -299
- data/lib/avo/app/resource_grid_fields.rb +0 -30
- data/lib/avo/app/services/authorization_service.rb +0 -110
- data/lib/avo/app/services/panel_service.rb +0 -25
- data/lib/avo/app/tool.rb +0 -5
- data/lib/generators/avo/templates/filters/boolean_filter.rb +0 -17
- data/lib/generators/avo/templates/filters/select_filter.rb +0 -17
- data/lib/generators/avo/templates/resource/%plural_name%_controller.tt +0 -2
- data/lib/generators/avo/templates/resource/%singular_name%.tt +0 -20
- data/public/avo-packs/css/application-3598cfbb.css.br +0 -0
- data/public/avo-packs/js/application-6ba2bd1bca05d69b96e0.js.br +0 -0
- data/public/avo-packs/js/application-6ba2bd1bca05d69b96e0.js.gz +0 -0
- data/public/avo-packs/js/application-6ba2bd1bca05d69b96e0.js.map.br +0 -0
data/lib/avo/app/filter.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Avo
|
2
|
-
class Filter
|
3
|
-
attr_accessor :name
|
4
|
-
attr_accessor :component
|
5
|
-
attr_accessor :default
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@name ||= 'Filter'
|
9
|
-
@component ||= 'boolean-filter'
|
10
|
-
@default ||= ''
|
11
|
-
end
|
12
|
-
|
13
|
-
def apply_query(request, query, value)
|
14
|
-
value.symbolize_keys! if value.is_a? Hash
|
15
|
-
|
16
|
-
self.apply(request, query, value)
|
17
|
-
end
|
18
|
-
|
19
|
-
def id
|
20
|
-
self.class.name.underscore.gsub('/', '_')
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/avo/app/licensing/hq.rb
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
module Avo
|
2
|
-
class HQ
|
3
|
-
attr_accessor :current_request
|
4
|
-
|
5
|
-
ENDPOINT = 'https://avohq.io/api/v1/licenses/check' unless const_defined?(:ENDPOINT)
|
6
|
-
CACHE_KEY = 'avo.hq.response' unless const_defined?(:CACHE_KEY)
|
7
|
-
REQUEST_TIMEOUT = 5 unless const_defined?(:REQUEST_TIMEOUT) # seconds
|
8
|
-
|
9
|
-
def initialize(current_request)
|
10
|
-
@current_request = current_request
|
11
|
-
@cache_store = Avo::App.cache_store
|
12
|
-
end
|
13
|
-
|
14
|
-
def response
|
15
|
-
@hq_response or request
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
def request
|
20
|
-
return cached_response if has_cached_response
|
21
|
-
|
22
|
-
begin
|
23
|
-
perform_and_cache_request
|
24
|
-
rescue HTTParty::Error => exception
|
25
|
-
cache_and_return_error 'HTTP client error.', exception.message
|
26
|
-
rescue Net::OpenTimeout => exception
|
27
|
-
cache_and_return_error 'Request timeout.', exception.message
|
28
|
-
rescue SocketError => exception
|
29
|
-
cache_and_return_error 'Connection error.', exception.message
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def perform_and_cache_request
|
34
|
-
hq_response = perform_request
|
35
|
-
|
36
|
-
return cache_and_return_error 'Avo HQ Internal server error.', hq_response.body if hq_response.code == 500
|
37
|
-
|
38
|
-
cache_response 1.hour.to_i, hq_response.parsed_response if hq_response.code == 200
|
39
|
-
end
|
40
|
-
|
41
|
-
def cache_response(time, response)
|
42
|
-
response.merge!(
|
43
|
-
expiry: time,
|
44
|
-
**payload,
|
45
|
-
).stringify_keys!
|
46
|
-
|
47
|
-
@cache_store.write(CACHE_KEY, response, expires_in: time)
|
48
|
-
|
49
|
-
@hq_response = response
|
50
|
-
|
51
|
-
response
|
52
|
-
end
|
53
|
-
|
54
|
-
def perform_request
|
55
|
-
puts 'Performing request to avohq.io API to check license availability.'.inspect if Rails.env.development?
|
56
|
-
|
57
|
-
HTTParty.post ENDPOINT, body: payload.to_json, headers: { 'Content-type': 'application/json' }, timeout: REQUEST_TIMEOUT
|
58
|
-
end
|
59
|
-
|
60
|
-
def payload
|
61
|
-
{
|
62
|
-
license: Avo.configuration.license,
|
63
|
-
license_key: Avo.configuration.license_key,
|
64
|
-
avo_version: Avo::VERSION,
|
65
|
-
rails_version: Rails::VERSION::STRING,
|
66
|
-
ruby_version: RUBY_VERSION,
|
67
|
-
environment: Rails.env,
|
68
|
-
ip: current_request.ip,
|
69
|
-
host: current_request.host,
|
70
|
-
port: current_request.port,
|
71
|
-
}
|
72
|
-
end
|
73
|
-
|
74
|
-
def cache_and_return_error(error, exception_message = '')
|
75
|
-
cache_response 5.minutes.to_i, { error: error, exception_message: exception_message }.stringify_keys
|
76
|
-
end
|
77
|
-
|
78
|
-
def has_cached_response
|
79
|
-
@cache_store.exist? CACHE_KEY
|
80
|
-
end
|
81
|
-
|
82
|
-
def cached_response
|
83
|
-
@cache_store.read CACHE_KEY
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module Avo
|
2
|
-
class License
|
3
|
-
attr_accessor :id
|
4
|
-
attr_accessor :response
|
5
|
-
attr_accessor :valid
|
6
|
-
|
7
|
-
def initialize(response)
|
8
|
-
@response = response
|
9
|
-
@id = response['id']
|
10
|
-
@valid = response['valid']
|
11
|
-
end
|
12
|
-
|
13
|
-
def valid?
|
14
|
-
valid
|
15
|
-
end
|
16
|
-
|
17
|
-
def invalid?
|
18
|
-
!valid?
|
19
|
-
end
|
20
|
-
|
21
|
-
def pro?
|
22
|
-
id == 'pro'
|
23
|
-
end
|
24
|
-
|
25
|
-
def error
|
26
|
-
@response['error']
|
27
|
-
end
|
28
|
-
|
29
|
-
def properties
|
30
|
-
@response.slice('valid', 'id', 'error').symbolize_keys
|
31
|
-
end
|
32
|
-
|
33
|
-
def abilities
|
34
|
-
[]
|
35
|
-
end
|
36
|
-
|
37
|
-
def can(ability)
|
38
|
-
abilities.include? ability
|
39
|
-
end
|
40
|
-
|
41
|
-
def cant(ability)
|
42
|
-
!can ability
|
43
|
-
end
|
44
|
-
|
45
|
-
alias_method :has, :can
|
46
|
-
alias_method :lacks, :cant
|
47
|
-
end
|
48
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require_relative 'license'
|
2
|
-
require_relative 'community_license'
|
3
|
-
require_relative 'pro_license'
|
4
|
-
require_relative 'null_license'
|
5
|
-
|
6
|
-
module Avo
|
7
|
-
class LicenseManager
|
8
|
-
def initialize(hq_response)
|
9
|
-
@hq_response = hq_response
|
10
|
-
end
|
11
|
-
|
12
|
-
def license
|
13
|
-
return NullLicense.new if Rails.env.test? and ENV['RUN_WITH_NULL_LICENSE'] == '1'
|
14
|
-
|
15
|
-
case @hq_response['id']
|
16
|
-
when 'community'
|
17
|
-
CommunityLicense.new @hq_response
|
18
|
-
when 'pro'
|
19
|
-
ProLicense.new @hq_response
|
20
|
-
else
|
21
|
-
NullLicense.new @hq_response
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
data/lib/avo/app/resource.rb
DELETED
@@ -1,299 +0,0 @@
|
|
1
|
-
require_relative 'resource_grid_fields'
|
2
|
-
require_relative 'fields/field'
|
3
|
-
|
4
|
-
module Avo
|
5
|
-
module Resources
|
6
|
-
class Resource
|
7
|
-
attr_writer :name
|
8
|
-
|
9
|
-
attr_accessor :id
|
10
|
-
attr_accessor :title
|
11
|
-
attr_accessor :search
|
12
|
-
attr_accessor :includes
|
13
|
-
attr_accessor :translation_key
|
14
|
-
attr_accessor :default_view_type
|
15
|
-
attr_accessor :devise_password_optional
|
16
|
-
attr_accessor :view
|
17
|
-
attr_accessor :model
|
18
|
-
attr_accessor :user
|
19
|
-
attr_accessor :fields_loader
|
20
|
-
attr_accessor :grid_loader
|
21
|
-
attr_accessor :actions_loader
|
22
|
-
attr_accessor :filters_loader
|
23
|
-
attr_accessor :params
|
24
|
-
|
25
|
-
alias :field :fields_loader
|
26
|
-
alias :f :field
|
27
|
-
alias :g :grid_loader
|
28
|
-
alias :action :actions_loader
|
29
|
-
alias :a :action
|
30
|
-
alias :filter :filters_loader
|
31
|
-
|
32
|
-
def initialize(request = nil)
|
33
|
-
@id = :id
|
34
|
-
@title = 'Resource'
|
35
|
-
@search = []
|
36
|
-
@includes = []
|
37
|
-
@translation_key = nil
|
38
|
-
@default_view_type = :table
|
39
|
-
@devise_password_optional = false
|
40
|
-
|
41
|
-
configure
|
42
|
-
boot_fields request
|
43
|
-
end
|
44
|
-
|
45
|
-
def boot_fields(request)
|
46
|
-
@fields_loader = Avo::FieldsLoader.new
|
47
|
-
fields request
|
48
|
-
|
49
|
-
@grid_loader = Avo::FieldsLoader.new
|
50
|
-
grid request if self.respond_to? :grid
|
51
|
-
|
52
|
-
@actions_loader = Avo::ActionsLoader.new
|
53
|
-
actions request if self.respond_to? :actions
|
54
|
-
|
55
|
-
@filters_loader = Avo::ActionsLoader.new
|
56
|
-
filters request if self.respond_to? :filters
|
57
|
-
end
|
58
|
-
|
59
|
-
def hydrate(model: nil, view: nil, user: nil, params: nil)
|
60
|
-
@view = view if view.present?
|
61
|
-
@user = user if user.present?
|
62
|
-
@params = params if params.present?
|
63
|
-
|
64
|
-
if model.present?
|
65
|
-
@model = model
|
66
|
-
|
67
|
-
hydrate_model_with_default_values if @view == :new
|
68
|
-
end
|
69
|
-
|
70
|
-
self
|
71
|
-
end
|
72
|
-
|
73
|
-
def get_fields(panel: nil, view_type: :table, reflection: nil)
|
74
|
-
case view_type.to_sym
|
75
|
-
when :table
|
76
|
-
fields = get_field_definitions.select do |field|
|
77
|
-
field.send("show_on_#{@view.to_s}")
|
78
|
-
end
|
79
|
-
.select do |field|
|
80
|
-
field.can_see.present? ? field.can_see.call : true
|
81
|
-
end
|
82
|
-
.select do |field|
|
83
|
-
unless field.respond_to?(:foreign_key) &&
|
84
|
-
reflection.present? &&
|
85
|
-
reflection.respond_to?(:foreign_key) &&
|
86
|
-
reflection.foreign_key == field.foreign_key
|
87
|
-
true
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
when :grid
|
92
|
-
fields = @grid_loader.bag
|
93
|
-
end
|
94
|
-
|
95
|
-
if panel.present?
|
96
|
-
fields = fields.select do |field|
|
97
|
-
field.panel_name == panel
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
fields = fields.map do |field|
|
102
|
-
field.hydrate(model: @model, view: @view, resource: self)
|
103
|
-
end
|
104
|
-
|
105
|
-
fields
|
106
|
-
end
|
107
|
-
|
108
|
-
def get_field_definitions
|
109
|
-
@fields_loader.bag.map do |field|
|
110
|
-
field.hydrate(resource: self, panel_name: default_panel_name, user: user)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def default_panel_name
|
115
|
-
return @params[:related_name].capitalize if @params[:related_name].present?
|
116
|
-
|
117
|
-
case @view
|
118
|
-
when :show
|
119
|
-
I18n.t('avo.resource_details', item: self.name.downcase, title: model_title).upcase_first
|
120
|
-
when :edit
|
121
|
-
I18n.t('avo.update_item', item: self.name.downcase, title: model_title).upcase_first
|
122
|
-
when :new
|
123
|
-
I18n.t('avo.create_new_item', item: self.name.downcase).upcase_first
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def panels
|
128
|
-
panels = [
|
129
|
-
{
|
130
|
-
name: default_panel_name,
|
131
|
-
type: :fields,
|
132
|
-
in_panel: true,
|
133
|
-
}
|
134
|
-
]
|
135
|
-
|
136
|
-
panels
|
137
|
-
end
|
138
|
-
|
139
|
-
def model_class
|
140
|
-
return @model.class if @model.present?
|
141
|
-
|
142
|
-
self.class.name.demodulize.safe_constantize
|
143
|
-
end
|
144
|
-
|
145
|
-
def model_title
|
146
|
-
return @model.send title if @model.present?
|
147
|
-
|
148
|
-
name
|
149
|
-
end
|
150
|
-
|
151
|
-
def name
|
152
|
-
return @name if @name.present?
|
153
|
-
|
154
|
-
return I18n.t(@translation_key, count: 1).capitalize if @translation_key
|
155
|
-
|
156
|
-
self.class.name.demodulize.titlecase
|
157
|
-
end
|
158
|
-
|
159
|
-
def singular_name
|
160
|
-
name
|
161
|
-
end
|
162
|
-
|
163
|
-
def plural_name
|
164
|
-
return I18n.t(@translation_key, count: 2).capitalize if @translation_key
|
165
|
-
|
166
|
-
name.pluralize
|
167
|
-
end
|
168
|
-
|
169
|
-
def underscore_name
|
170
|
-
return @name if @name.present?
|
171
|
-
|
172
|
-
self.class.name.demodulize.underscore
|
173
|
-
end
|
174
|
-
|
175
|
-
def available_view_types
|
176
|
-
get_fields(view_type: :grid).length > 0 ? [:grid, :table] : [:table]
|
177
|
-
end
|
178
|
-
|
179
|
-
def get_filters
|
180
|
-
@filters_loader.bag
|
181
|
-
end
|
182
|
-
|
183
|
-
def get_actions
|
184
|
-
@actions_loader.bag
|
185
|
-
end
|
186
|
-
|
187
|
-
def route_key
|
188
|
-
model_class.model_name.route_key
|
189
|
-
end
|
190
|
-
|
191
|
-
def query_search(query: '', via_resource_name: , via_resource_id:, user:)
|
192
|
-
# model_class = self.model
|
193
|
-
|
194
|
-
db_query = AuthorizationService.apply_policy(user, model_class)
|
195
|
-
|
196
|
-
if via_resource_name.present?
|
197
|
-
related_model = App.get_resource_by_name(via_resource_name).model
|
198
|
-
|
199
|
-
db_query = related_model.find(via_resource_id).public_send(self.plural_name.downcase)
|
200
|
-
end
|
201
|
-
|
202
|
-
new_query = []
|
203
|
-
|
204
|
-
[self.search].flatten.each_with_index do |search_by, index|
|
205
|
-
new_query.push 'or' if index != 0
|
206
|
-
|
207
|
-
new_query.push "text(#{search_by}) ILIKE '%#{query}%'"
|
208
|
-
end
|
209
|
-
|
210
|
-
db_query.where(new_query.join(' '))
|
211
|
-
end
|
212
|
-
|
213
|
-
def attached_file_fields
|
214
|
-
get_field_definitions.select do |field|
|
215
|
-
[Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
def fill_model(model, params)
|
220
|
-
# Map the received params to their actual fields
|
221
|
-
fields_by_database_id = self.get_field_definitions.map { |field| [field.database_id(model).to_s, field] }.to_h
|
222
|
-
|
223
|
-
params.each do |key, value|
|
224
|
-
field = fields_by_database_id[key]
|
225
|
-
|
226
|
-
next unless field
|
227
|
-
|
228
|
-
model = field.fill_field model, key, value
|
229
|
-
end
|
230
|
-
|
231
|
-
model
|
232
|
-
end
|
233
|
-
|
234
|
-
def authorization
|
235
|
-
AuthorizationService.new(user, model)
|
236
|
-
end
|
237
|
-
|
238
|
-
def file_hash
|
239
|
-
content_to_be_hashed = ''
|
240
|
-
|
241
|
-
# resource file hash
|
242
|
-
resource_path = Rails.root.join('app', 'avo', 'resources', "#{name.underscore}.rb").to_s
|
243
|
-
if File.file? resource_path
|
244
|
-
content_to_be_hashed += File.read(resource_path)
|
245
|
-
end
|
246
|
-
|
247
|
-
# policy file hash
|
248
|
-
policy_path = Rails.root.join('app', 'policies', "#{name.underscore}_policy.rb").to_s
|
249
|
-
if File.file? policy_path
|
250
|
-
content_to_be_hashed += File.read(policy_path)
|
251
|
-
end
|
252
|
-
|
253
|
-
Digest::MD5.hexdigest(content_to_be_hashed)
|
254
|
-
end
|
255
|
-
|
256
|
-
def cache_hash(parent_model)
|
257
|
-
if parent_model.present?
|
258
|
-
[self.model, self.file_hash, parent_model]
|
259
|
-
else
|
260
|
-
[self.model, self.file_hash]
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
# For :new views we're hydrating the model with the values from the resource's default attribute.
|
265
|
-
# We will not overwrite any attributes that come pre-filled in the model.
|
266
|
-
def hydrate_model_with_default_values
|
267
|
-
default_values = get_fields.select do |field|
|
268
|
-
!field.computed
|
269
|
-
end
|
270
|
-
.map do |field|
|
271
|
-
id = field.id
|
272
|
-
value = field.value
|
273
|
-
|
274
|
-
if field.respond_to? :foreign_key
|
275
|
-
id = field.foreign_key.to_sym
|
276
|
-
|
277
|
-
reflection = @model._reflections[@params[:via_relation]]
|
278
|
-
|
279
|
-
if reflection.present? && reflection.foreign_key.present?
|
280
|
-
value = @params[:via_resource_id]
|
281
|
-
end
|
282
|
-
end
|
283
|
-
|
284
|
-
[id, value]
|
285
|
-
end
|
286
|
-
.to_h
|
287
|
-
.select do |id, value|
|
288
|
-
value.present?
|
289
|
-
end
|
290
|
-
|
291
|
-
default_values.each do |id, value|
|
292
|
-
if @model.send(id).nil?
|
293
|
-
@model.send("#{id}=", value)
|
294
|
-
end
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
299
|
-
end
|