carnival 0.2.7 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-regular-webfont.eot +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-regular-webfont.svg +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-regular-webfont.ttf +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-regular-webfont.woff +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-semibold-webfont.eot +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-semibold-webfont.svg +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-semibold-webfont.ttf +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/opensans-semibold-webfont.woff +0 -0
- data/app/assets/{images/carnival/fonts → fonts/carnival}/up-not.png +0 -0
- data/app/assets/javascripts/carnival/{vizir_admin.js → vizir_admin.js.erb} +44 -18
- data/app/assets/stylesheets/carnival/actions.css.scss +3 -7
- data/app/assets/stylesheets/carnival/carnival-table.css.scss +12 -3
- data/app/assets/stylesheets/carnival/forms.css.scss +1 -0
- data/app/assets/stylesheets/carnival/menu.css.scss +3 -3
- data/app/assets/stylesheets/carnival/period.css.scss +1 -1
- data/app/assets/stylesheets/carnival/search.css.scss +9 -6
- data/app/assets/stylesheets/carnival/structure.css.scss +10 -10
- data/app/controllers/carnival/base_admin_controller.rb +37 -57
- data/app/helpers/carnival/base_admin_helper.rb +52 -70
- data/app/inputs/admin_date_input.rb +2 -1
- data/app/models/carnival/batch_action.rb +1 -2
- data/app/models/carnival/field.rb +3 -2
- data/app/presenters/carnival/base_admin_presenter.rb +47 -192
- data/app/presenters/carnival/dsl.rb +55 -0
- data/app/services/carnival/klass_service.rb +5 -1
- data/app/services/carnival/presenters/advanced_search_parser.rb +4 -5
- data/app/services/carnival/query_service.rb +15 -24
- data/app/view_objects/carnival/paginator.rb +10 -12
- data/app/view_objects/carnival/thead_renderer.rb +11 -18
- data/app/views/carnival/base_admin/_index_as_table.html.haml +6 -4
- data/app/views/carnival/base_admin/index.html.haml +8 -4
- data/app/views/carnival/shared/_advanced_search.html.haml +6 -5
- data/app/views/carnival/shared/form/_form.html.haml +3 -2
- data/config/locales/carnival.en.yml +13 -5
- data/config/locales/carnival.pt-br.yml +1 -1
- data/lib/carnival.rb +10 -12
- data/lib/carnival/engine.rb +3 -2
- data/lib/carnival/routes.rb +4 -4
- data/lib/carnival/version.rb +1 -1
- data/spec/dummy/app/controllers/admin/todo_lists_controller.rb +7 -0
- data/spec/dummy/app/controllers/admin/todos_controller.rb +7 -0
- data/spec/dummy/app/models/todo.rb +7 -0
- data/spec/dummy/app/models/todo_list.rb +4 -0
- data/spec/dummy/app/presenters/admin/todo_list_presenter.rb +13 -0
- data/spec/dummy/app/presenters/admin/todo_presenter.rb +32 -0
- data/spec/dummy/config/application.rb +3 -0
- data/spec/dummy/config/database.yml +1 -1
- data/spec/dummy/config/environments/development.rb +2 -0
- data/spec/dummy/config/initializers/carnival_initializer.rb +25 -0
- data/spec/dummy/config/locales/carnival.en.yml +28 -0
- data/spec/dummy/config/locales/carnival.pt-br.yml +259 -0
- data/spec/dummy/config/routes.rb +7 -52
- data/spec/dummy/db/migrate/20150316021645_create_todos.rb +10 -0
- data/spec/dummy/db/migrate/20150408165317_create_todo_lists.rb +8 -0
- data/spec/dummy/db/migrate/20150408165333_add_todo_list_relation_to_todos.rb +5 -0
- data/spec/dummy/db/schema.rb +32 -0
- data/spec/factories/todo_lists_factory.rb +5 -0
- data/spec/factories/todos_factory.rb +18 -0
- data/spec/features/create_spec.rb +28 -0
- data/spec/features/destroy_spec.rb +11 -0
- data/spec/features/index/basic_index_spec.rb +14 -0
- data/spec/features/index/pagination_spec.rb +19 -0
- data/spec/features/index/scope_spec.rb +29 -0
- data/spec/features/index/search_spec.rb +28 -0
- data/spec/features/show_spec.rb +15 -0
- data/spec/features/update_spec.rb +26 -0
- data/spec/rails_helper.rb +25 -5
- data/spec/support/features/index_helpers.rb +15 -0
- metadata +386 -284
- checksums.yaml +0 -7
- data/app/views/carnival/base_admin/index.pdf.haml +0 -13
- data/app/views/carnival/shared/_header.pdf.haml +0 -13
- data/app/views/carnival/shared/_list_cel.pdf.haml +0 -9
- data/app/views/carnival/shared/_report.pdf.haml +0 -21
- data/config/initializers/wicked_pdf.rb +0 -5
@@ -1,7 +1,7 @@
|
|
1
1
|
module Carnival
|
2
2
|
module BaseAdminHelper
|
3
3
|
def set_resource_actived_if_current(path)
|
4
|
-
(request.fullpath == path)? 'actived' : ''
|
4
|
+
(request.fullpath == path) ? 'actived' : ''
|
5
5
|
end
|
6
6
|
|
7
7
|
def show_messages
|
@@ -18,38 +18,36 @@ module Carnival
|
|
18
18
|
''
|
19
19
|
end
|
20
20
|
|
21
|
-
def caminho_modelo(action, extra_params=nil)
|
22
|
-
params = {controller: controller.controller_name, action: action}
|
21
|
+
def caminho_modelo(action, extra_params = nil)
|
22
|
+
params = { controller: controller.controller_name, action: action }
|
23
23
|
params = params.merge(extra_params) if extra_params.present?
|
24
24
|
url_for(params)
|
25
25
|
end
|
26
26
|
|
27
|
-
def carnival_render_if_exist
|
28
|
-
if partial_exist?(partial_path)
|
29
|
-
return render partial_path
|
30
|
-
end
|
27
|
+
def carnival_render_if_exist(partial_path)
|
28
|
+
render partial_path if partial_exist?(partial_path)
|
31
29
|
end
|
32
30
|
|
33
|
-
def partial_exist?
|
34
|
-
File.
|
31
|
+
def partial_exist?(partial_path)
|
32
|
+
File.exist?(get_partial_path(partial_path))
|
35
33
|
end
|
36
34
|
|
37
|
-
def has_many_relation?
|
35
|
+
def has_many_relation?(model, field)
|
38
36
|
klass = Carnival::KlassService.new model.class
|
39
37
|
klass.is_a_has_many_relation?(field.to_sym)
|
40
38
|
end
|
41
39
|
|
42
|
-
def has_one_relation?
|
40
|
+
def has_one_relation?(model, field)
|
43
41
|
klass = Carnival::KlassService.new model.class
|
44
42
|
klass.is_a_has_one_relation?(field.to_sym)
|
45
43
|
end
|
46
44
|
|
47
|
-
def many_to_many_relation?
|
45
|
+
def many_to_many_relation?(model, field)
|
48
46
|
klass = Carnival::KlassService.new model.class
|
49
47
|
klass.is_a_many_to_many_relation?(field.to_sym)
|
50
48
|
end
|
51
49
|
|
52
|
-
def get_partial_path
|
50
|
+
def get_partial_path(partial_path)
|
53
51
|
path = Rails.root.join('app', 'views')
|
54
52
|
partial_path_array = partial_path.split('/')
|
55
53
|
partial_path_array.each do |pp|
|
@@ -62,9 +60,9 @@ module Carnival
|
|
62
60
|
path
|
63
61
|
end
|
64
62
|
|
65
|
-
def menu_link
|
63
|
+
def menu_link(link)
|
66
64
|
link.strip!
|
67
|
-
if link.to_s.end_with?('_path')
|
65
|
+
if link.to_s.end_with?('_path') || link.to_s.end_with?('_url')
|
68
66
|
return eval link
|
69
67
|
elsif link.index(/_path.+/) #path with arguments
|
70
68
|
return eval link
|
@@ -87,7 +85,7 @@ module Carnival
|
|
87
85
|
end
|
88
86
|
|
89
87
|
def field_to_show(presenter, field_name, record, show_only_values=false)
|
90
|
-
if presenter.fields[field_name].params[:as] == :partial
|
88
|
+
if presenter.fields[field_name].params[:as] == :partial
|
91
89
|
raw(render field_name.to_s, record: record)
|
92
90
|
else
|
93
91
|
rendered = field_value_and_type presenter, field_name, record
|
@@ -96,49 +94,47 @@ module Carnival
|
|
96
94
|
|
97
95
|
is_relation = presenter.relation_field?(field_name)
|
98
96
|
|
99
|
-
|
97
|
+
if value.present?
|
100
98
|
formatted_field = format_field(presenter, field_name, field_type, value)
|
101
|
-
if is_relation
|
99
|
+
if is_relation && !show_only_values
|
102
100
|
link_to(formatted_field, presenter.relation_path(field_name, record))
|
103
101
|
else
|
104
102
|
formatted_field
|
105
103
|
end
|
106
|
-
else
|
107
|
-
nil
|
108
104
|
end
|
109
105
|
end
|
110
106
|
end
|
111
107
|
|
112
|
-
def field_value_and_type
|
108
|
+
def field_value_and_type(presenter, field_name, record)
|
113
109
|
renderer = FieldRenderers::RendererCreator
|
114
|
-
|
110
|
+
.create_field_renderer(presenter, field_name)
|
115
111
|
|
116
|
-
|
112
|
+
renderer.render_field(record)
|
117
113
|
end
|
118
114
|
|
119
|
-
def is_image?
|
115
|
+
def is_image?(field_type, value)
|
120
116
|
field_type.nil? and value.class.to_s == "Paperclip::Attachment" and value.content_type.include? "image"
|
121
117
|
end
|
122
118
|
|
123
119
|
def format_field(presenter, field_name, field_type, value)
|
124
120
|
if is_image?(field_type, value)
|
125
|
-
|
121
|
+
image_tag value.url(:thumb), alt: translate_field(presenter, field_name), class: 'attr'
|
126
122
|
else
|
127
123
|
case field_type
|
128
124
|
when :datetime
|
129
125
|
begin
|
130
126
|
I18n.l(value, format: :long_date)
|
131
127
|
rescue I18n::MissingTranslationData
|
132
|
-
value.strftime(
|
128
|
+
value.strftime('%d/%m/%y %H:%M:%S')
|
133
129
|
end
|
134
130
|
when :date
|
135
131
|
begin
|
136
132
|
I18n.l(value, format: :short_date)
|
137
133
|
rescue I18n::MissingTranslationData
|
138
|
-
value.strftime(
|
134
|
+
value.strftime('%d/%m/%Y')
|
139
135
|
end
|
140
136
|
when :number
|
141
|
-
number_with_precision(value, :
|
137
|
+
number_with_precision(value, precision: 2, separator: ',')
|
142
138
|
when :enum
|
143
139
|
presenter.model_class.const_get(field_name.upcase)[value]
|
144
140
|
else
|
@@ -150,7 +146,7 @@ module Carnival
|
|
150
146
|
def translate_field(presenter, field_name)
|
151
147
|
field = presenter.get_field(field_name)
|
152
148
|
field_key = field.name_for_translation
|
153
|
-
presenter.
|
149
|
+
presenter.model_class.human_attribute_name field_key
|
154
150
|
end
|
155
151
|
|
156
152
|
def list_cel(presenter, field, record, only_render_fields)
|
@@ -174,68 +170,54 @@ module Carnival
|
|
174
170
|
"<div class='attr'><div class='label #{field}'>#{translate_field(presenter, field)}:</div><div class='field_value #{field}'>#{result}</div></div>"
|
175
171
|
end
|
176
172
|
|
177
|
-
def get_css_class
|
173
|
+
def get_css_class(presenter, field, record)
|
178
174
|
css_class = presenter.get_field(field).css_class
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
175
|
+
if css_class.is_a? Hash
|
176
|
+
record.send(css_class[:method])
|
177
|
+
else css_class
|
178
|
+
css_class.presence || ''
|
179
|
+
end
|
183
180
|
end
|
184
181
|
|
185
182
|
def list_buttons(presenter, record)
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
end
|
183
|
+
if presenter.actions_for_record.any?
|
184
|
+
presenter.actions_for_record.map do |_, record_action|
|
185
|
+
next unless presenter.render_action?(record, record_action, params[:action])
|
186
|
+
next unless record_action.show(record)
|
187
|
+
if record_action.remote?
|
188
|
+
button_action_remote(record_action, presenter, record)
|
189
|
+
else
|
190
|
+
button_action(record_action, presenter, record)
|
195
191
|
end
|
196
|
-
end
|
192
|
+
end.reject(&:nil?).inject(:+)
|
193
|
+
else
|
194
|
+
""
|
197
195
|
end
|
198
|
-
result
|
199
196
|
end
|
200
197
|
|
201
198
|
def button_action(action, presenter, record)
|
202
199
|
label = t("#{presenter.model_name.underscore}.#{action.name}", default: t("carnival.#{action.name}"))
|
203
|
-
path = action.path(presenter, :
|
200
|
+
path = action.path(presenter, id: record.id)
|
201
|
+
|
204
202
|
if action.default_partial == :default
|
205
|
-
|
203
|
+
link_to label, path, class: "carnival-action-link #{label.parameterize}"
|
206
204
|
elsif action.default_partial == :delete
|
207
|
-
confirm
|
208
|
-
"<a class='carnival-action-link apagar' data-confirm='#{confirm}' data-method='delete' href='#{path}' rel='nofollow'>#{label}</a>"
|
205
|
+
link_to label, path, class: 'carnival-action-link apagar', method: :delete, data: { confirm: I18n.t('are_you_sure') }
|
209
206
|
end
|
210
207
|
end
|
211
208
|
|
212
209
|
def button_action_remote(action, presenter, record)
|
213
|
-
name = action.name
|
214
210
|
params = action.params
|
215
211
|
label = t("#{presenter.model_name}.#{action.name}", default: t("carnival.#{action.name}"))
|
216
|
-
path = action.path(presenter, :
|
212
|
+
path = action.path(presenter, id: record.id)
|
217
213
|
|
218
|
-
success_callback = "#{name}_success_callback"
|
219
|
-
|
220
|
-
|
221
|
-
end
|
222
|
-
|
223
|
-
error_callback = "#{name}_error_callback"
|
224
|
-
if params[:error]
|
225
|
-
error_callback = params[:error]
|
226
|
-
end
|
214
|
+
success_callback = params[:success] || "#{action.name}_success_callback"
|
215
|
+
error_callback = params[:error] || "#{action.name}_error_callback"
|
216
|
+
params[:show_overlay] = params[:show_overlay].presence || false
|
227
217
|
|
228
|
-
|
229
|
-
if params[:show_overlay]
|
230
|
-
data_overlay = 'data-carnival-show-overlay=true'
|
231
|
-
end
|
218
|
+
remote_function = "Carnival.remoteFunction('#{path}', '#{success_callback}', '#{error_callback}', '#{params[:method]}', {} , #{params[:show_overlay]})"
|
232
219
|
|
233
|
-
|
234
|
-
|
235
|
-
remote_function = "Carnival.remoteFunction(\"#{path}\", \"#{success_callback}\", \"#{error_callback}\", \"#{params[:method]}\", {} ,#{params[:show_overlay]})"
|
236
|
-
|
237
|
-
"<a class='carnival-action-link editar' href='javascript:#{remote_function}'>#{label}</a>"
|
220
|
+
link_to label, "javascript: #{remote_function}", class: 'carnival-action-link editar'
|
238
221
|
end
|
239
|
-
|
240
222
|
end
|
241
223
|
end
|
@@ -3,7 +3,8 @@ class AdminDateInput < SimpleForm::Inputs::Base
|
|
3
3
|
include AdminBaseInput
|
4
4
|
|
5
5
|
def input(wrapper_options)
|
6
|
-
|
6
|
+
f = I18n.t('date.formats.default')
|
7
|
+
format = if column.type == :datetime then "#{f} %H:%M" else "#{f}" end
|
7
8
|
default_input_html_options = {class: "#{column.type}picker",
|
8
9
|
value: @builder.object.send(attribute_name).nil? ? "" : @builder.object.send(attribute_name).strftime(format)}
|
9
10
|
options = merge_options(default_input_html_options, input_html_options)
|
@@ -9,7 +9,6 @@ module Carnival
|
|
9
9
|
@path = params[:path] if params[:path].present?
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
12
|
def params
|
14
13
|
@params
|
15
14
|
end
|
@@ -19,7 +18,7 @@ module Carnival
|
|
19
18
|
end
|
20
19
|
|
21
20
|
def to_label
|
22
|
-
I18n.t("#{@presenter.model_name}.#{name}")
|
21
|
+
I18n.t("#{@presenter.model_name}.#{name}")
|
23
22
|
end
|
24
23
|
|
25
24
|
end
|
@@ -66,6 +66,7 @@ module Carnival
|
|
66
66
|
}
|
67
67
|
end
|
68
68
|
end
|
69
|
+
|
69
70
|
def default_sortable?
|
70
71
|
@params[:sortable] && @params[:sortable].class == Hash && @params[:sortable][:default] == true
|
71
72
|
end
|
@@ -157,8 +158,8 @@ module Carnival
|
|
157
158
|
end
|
158
159
|
|
159
160
|
def sort_name
|
160
|
-
return @params[:related_to].to_s if @params[:related_to].present?
|
161
|
-
@name.to_s
|
161
|
+
return @params[:related_to].to_s if @params[:related_to].present?
|
162
|
+
@name.to_s
|
162
163
|
end
|
163
164
|
|
164
165
|
private
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
module Carnival
|
3
3
|
class BaseAdminPresenter
|
4
|
+
include Dsl
|
4
5
|
include Rails.application.routes.url_helpers
|
5
6
|
|
6
7
|
def initialize(params)
|
@@ -24,23 +25,6 @@ module Carnival
|
|
24
25
|
self.class.to_s
|
25
26
|
end
|
26
27
|
|
27
|
-
@@index_as = {}
|
28
|
-
def self.index_as(type)
|
29
|
-
@@index_as[presenter_class_name] = type
|
30
|
-
end
|
31
|
-
|
32
|
-
@@actions = {}
|
33
|
-
def self.action(name, params = {})
|
34
|
-
@@actions[presenter_class_name] ||= {}
|
35
|
-
@@actions[presenter_class_name][name] = Carnival::Action.new(name, params)
|
36
|
-
end
|
37
|
-
|
38
|
-
@@batch_actions = {}
|
39
|
-
def self.batch_action(name, params = {})
|
40
|
-
@@batch_actions[presenter_class_name] ||= {}
|
41
|
-
@@batch_actions[presenter_class_name][name] = Carnival::BatchAction.new(self.new({}), name, params)
|
42
|
-
end
|
43
|
-
|
44
28
|
def has_batch_actions?
|
45
29
|
if @@batch_actions[presenter_class_name].present?
|
46
30
|
@@batch_actions[presenter_class_name].keys.size > 0
|
@@ -49,12 +33,6 @@ module Carnival
|
|
49
33
|
end
|
50
34
|
end
|
51
35
|
|
52
|
-
@@items_per_page = {}
|
53
|
-
def self.items_per_page(per_page)
|
54
|
-
@@items_per_page[presenter_class_name] ||= {}
|
55
|
-
@@items_per_page[presenter_class_name][:items_per_page] = per_page
|
56
|
-
end
|
57
|
-
|
58
36
|
def items_per_page
|
59
37
|
@@items_per_page[presenter_class_name] ||= {}
|
60
38
|
unless @@items_per_page[presenter_class_name][:items_per_page].present? and @@items_per_page[presenter_class_name][:items_per_page] > 0
|
@@ -63,16 +41,10 @@ module Carnival
|
|
63
41
|
@@items_per_page[presenter_class_name][:items_per_page]
|
64
42
|
end
|
65
43
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
def index_as_list?
|
71
|
-
index_as == :list
|
72
|
-
end
|
73
|
-
|
74
|
-
def index_as_grid?
|
75
|
-
index_as == :grid
|
44
|
+
[:table, :list, :grid].each do |index_type|
|
45
|
+
define_method "index_as_#{index_type}?" do
|
46
|
+
index_as == index_type
|
47
|
+
end
|
76
48
|
end
|
77
49
|
|
78
50
|
def index_as
|
@@ -99,11 +71,6 @@ module Carnival
|
|
99
71
|
@@actions[presenter_class_name].present? && @@actions[presenter_class_name][action].present?
|
100
72
|
end
|
101
73
|
|
102
|
-
@@model_names = {}
|
103
|
-
@@fields = {}
|
104
|
-
@@scopes = {}
|
105
|
-
@@forms = {}
|
106
|
-
|
107
74
|
def scopes
|
108
75
|
@@scopes[presenter_class_name] || {}
|
109
76
|
end
|
@@ -131,13 +98,16 @@ module Carnival
|
|
131
98
|
end
|
132
99
|
|
133
100
|
def form_for(action)
|
134
|
-
|
135
|
-
|
101
|
+
if @@forms[presenter_class_name].present? &&
|
102
|
+
@@forms[presenter_class_name][action].present?
|
103
|
+
form = @@forms[presenter_class_name][action]
|
104
|
+
else
|
105
|
+
form = Carnival::Form.new(action)
|
106
|
+
end
|
136
107
|
form.fields = fields_for_action(action)
|
137
108
|
form
|
138
109
|
end
|
139
110
|
|
140
|
-
|
141
111
|
def model_name
|
142
112
|
if @@model_names[presenter_class_name].nil?
|
143
113
|
self.class.to_s.split("::").last().gsub("Presenter", "").underscore
|
@@ -194,33 +164,18 @@ module Carnival
|
|
194
164
|
model_class.table_name
|
195
165
|
end
|
196
166
|
|
197
|
-
def searchable_fields
|
198
|
-
searchable_fields = {}
|
199
|
-
@@fields[presenter_class_name].each do |key, field|
|
200
|
-
if relation_field? key
|
201
|
-
field_name = "#{field.name.pluralize}.name"
|
202
|
-
else
|
203
|
-
field_name = "#{self.table_name}.#{field.name}"
|
204
|
-
end
|
205
|
-
searchable_fields[field_name] = field if field.searchable?
|
206
|
-
end
|
207
|
-
searchable_fields
|
208
|
-
end
|
209
|
-
|
210
167
|
def join_tables
|
211
|
-
|
212
|
-
|
213
|
-
joins << field.association_name if relation_field? key.to_sym
|
168
|
+
@@fields[presenter_class_name].map do |key, field|
|
169
|
+
field.association_name if relation_field? key.to_sym
|
214
170
|
end
|
215
|
-
joins
|
216
171
|
end
|
217
172
|
|
218
173
|
def render_field?(field, action = nil)
|
219
|
-
|
174
|
+
true
|
220
175
|
end
|
221
176
|
|
222
177
|
def render_action?(record, record_action, action = nil)
|
223
|
-
|
178
|
+
true
|
224
179
|
end
|
225
180
|
|
226
181
|
def build_relation_field(field, model_object)
|
@@ -232,49 +187,32 @@ module Carnival
|
|
232
187
|
end
|
233
188
|
|
234
189
|
def must_render_field?(nested_in, field, model_object)
|
235
|
-
|
236
|
-
if
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
end
|
190
|
+
return true unless nested_in.present?
|
191
|
+
if field.as == :partial
|
192
|
+
true
|
193
|
+
elsif nested_in.class == model_object.send(field.name).class ||
|
194
|
+
nested_in.class.name.underscore.split('/').last == field.name
|
195
|
+
false
|
196
|
+
else
|
197
|
+
true
|
244
198
|
end
|
245
|
-
return must_render
|
246
199
|
end
|
247
200
|
|
248
201
|
def default_scope
|
249
202
|
return if scopes.empty?
|
250
|
-
@@scopes[presenter_class_name].
|
251
|
-
|
252
|
-
end
|
253
|
-
@@scopes[presenter_class_name].values.first
|
203
|
+
@@scopes[presenter_class_name].values.find(&:default?) ||
|
204
|
+
@@scopes[presenter_class_name].values.first
|
254
205
|
end
|
255
206
|
|
256
207
|
def default_sortable_field
|
257
|
-
|
258
|
-
|
259
|
-
default_field = field if field.default_sortable?
|
260
|
-
break
|
261
|
-
end
|
262
|
-
default_field = @@fields[presenter_class_name].first[1] if default_field.nil?
|
263
|
-
default_field
|
208
|
+
@@fields[presenter_class_name].values.find(&:default_sortable?) ||
|
209
|
+
@@fields[presenter_class_name].values.first
|
264
210
|
end
|
265
211
|
|
266
212
|
def default_sort_direction
|
267
213
|
default_sortable_field.default_sort_direction
|
268
214
|
end
|
269
215
|
|
270
|
-
def sortable_fields
|
271
|
-
sortable_fields = {}
|
272
|
-
@@fields[presenter_class_name].each do |key, field|
|
273
|
-
sortable_fields[key] = field if field.sortable?
|
274
|
-
end
|
275
|
-
sortable_fields
|
276
|
-
end
|
277
|
-
|
278
216
|
def advanced_search_fields
|
279
217
|
fields = @@fields[presenter_class_name]
|
280
218
|
@advanced_search_parser.get_advanced_search_fields fields
|
@@ -348,14 +286,14 @@ module Carnival
|
|
348
286
|
return nil if !relation_field?(field)
|
349
287
|
relation_name = get_related_class_for_field(field)
|
350
288
|
related = record.send(field.association_name)
|
351
|
-
|
289
|
+
if related.present?
|
352
290
|
if is_one_to_one_relation?(field)
|
353
|
-
params = {:
|
291
|
+
params = { controller: "#{extract_namespace.downcase}/#{relation_name}", action: :show, id: related.id }
|
354
292
|
else
|
355
|
-
params = {:
|
293
|
+
params = { controller: "#{extract_namespace.downcase}/#{get_association_from_field(field)}", action: :index, advanced_search: make_relation_advanced_query_url_options(field.name, record) }
|
356
294
|
end
|
357
|
-
params = params.merge(:
|
358
|
-
|
295
|
+
params = params.merge(only_path: true)
|
296
|
+
generate_route_path params
|
359
297
|
else
|
360
298
|
'#'
|
361
299
|
end
|
@@ -397,142 +335,59 @@ module Carnival
|
|
397
335
|
def controller_to_field_sym field
|
398
336
|
"#{extract_namespace}::#{field.to_s.classify.pluralize}Controller".constantize.send("new")
|
399
337
|
end
|
338
|
+
|
400
339
|
def load_dependent_select_options_path
|
401
340
|
if extract_namespace.present?
|
402
341
|
"/#{extract_namespace.downcase}/carnival-base/load_dependent_select_options"
|
403
342
|
else
|
404
|
-
|
343
|
+
'/carnival-base/load_dependent_select_options'
|
405
344
|
end
|
406
345
|
end
|
407
346
|
|
408
347
|
protected
|
348
|
+
|
409
349
|
def make_relation_advanced_query_url_options(field, record)
|
410
350
|
relation_model = @klass_service.get_association(field).klass.name.pluralize.underscore.split("/").last
|
411
351
|
relation_field = @klass_service.get_association(field).foreign_key
|
412
352
|
relation_value = record.id
|
413
|
-
{"#{relation_model}.#{relation_field}" => relation_value}
|
353
|
+
{ "#{relation_model}.#{relation_field}" => relation_value }
|
414
354
|
end
|
415
355
|
|
416
356
|
def filter_actions(default_actions, target)
|
417
357
|
actions = {}
|
418
|
-
return actions
|
358
|
+
return actions unless @@actions[presenter_class_name]
|
419
359
|
|
420
360
|
@@actions[presenter_class_name].each do |key, action|
|
421
|
-
if default_actions.include?(key) || (action.target == target && key != :new && key != :csv
|
361
|
+
if default_actions.include?(key) || (action.target == target && key != :new && key != :csv)
|
422
362
|
actions[key] = action
|
423
363
|
end
|
424
364
|
end
|
425
365
|
actions
|
426
366
|
end
|
427
367
|
|
428
|
-
def presenter_class_name
|
429
|
-
if self.class.name ==
|
430
|
-
|
368
|
+
def self.presenter_class_name
|
369
|
+
if self.class.name == 'Class'
|
370
|
+
to_s
|
431
371
|
else
|
432
372
|
self.class.name
|
433
373
|
end
|
434
374
|
end
|
435
375
|
|
436
|
-
def
|
437
|
-
|
438
|
-
return records if field_param["value"] == ""
|
439
|
-
|
440
|
-
if relation_field?(search_field.to_sym)
|
441
|
-
related_model = model_class.reflect_on_association(search_field.to_sym).klass.name.underscore
|
442
|
-
foreign_key = model_class.reflect_on_association(search_field.to_sym).foreign_key
|
443
|
-
if model_class.reflect_on_association(search_field.to_sym).macro == :belongs_to
|
444
|
-
records = records.joins(related_model.split("/").last.to_sym)
|
445
|
-
else
|
446
|
-
records = records.joins(related_model.split("/").last.pluralize)
|
447
|
-
end
|
448
|
-
table = related_model.split("/").last.pluralize
|
449
|
-
column = "id"
|
450
|
-
else
|
451
|
-
table = table_name
|
452
|
-
column = search_field
|
453
|
-
end
|
454
|
-
full_column_query = "#{table}.#{column}"
|
455
|
-
where_clause = nil
|
456
|
-
|
457
|
-
case field_param["operator"]
|
458
|
-
when "equal"
|
459
|
-
if field_param["value"] == "nil"
|
460
|
-
where_clause = "#{full_column_query} is null"
|
461
|
-
else
|
462
|
-
where_clause = "#{full_column_query} = '#{advanced_search_field_value_for_query(field_param["value"])}'"
|
463
|
-
end
|
464
|
-
when "like"
|
465
|
-
where_clause = "#{full_column_query} like '%#{field_param["value"]}%'"
|
466
|
-
when "greater_than"
|
467
|
-
where_clause = "#{full_column_query} >= '#{field_param["value"]}'"
|
468
|
-
when "less_than"
|
469
|
-
where_clause = "#{full_column_query} <= '#{field_param["value"]}'"
|
470
|
-
when "between"
|
471
|
-
where_clause = "#{full_column_query} between '#{field_param["value"]}' and '#{field_param["value2"]}'"
|
472
|
-
else
|
473
|
-
where_clause = "#{full_column_query} = #{advanced_search_field_value_for_query(field_param["value"])}"
|
474
|
-
end
|
475
|
-
records = records.where(where_clause) if where_clause.present?
|
476
|
-
records
|
477
|
-
end
|
478
|
-
|
479
|
-
def advanced_search_field_value_for_query(value)
|
480
|
-
if "true" == value.downcase
|
481
|
-
return "'t'"
|
482
|
-
elsif "false" == value.downcase
|
483
|
-
return "'f'"
|
484
|
-
else
|
485
|
-
"#{value}"
|
486
|
-
end
|
376
|
+
def presenter_class_name
|
377
|
+
self.class.presenter_class_name
|
487
378
|
end
|
488
379
|
|
489
380
|
def is_namespaced?
|
490
|
-
self.class.to_s.split(
|
381
|
+
self.class.to_s.split('::').size > 0
|
491
382
|
end
|
492
383
|
|
493
384
|
def extract_namespace
|
494
|
-
|
495
|
-
|
496
|
-
namespace = arr[0] if arr.size > 1
|
497
|
-
namespace
|
498
|
-
end
|
499
|
-
|
500
|
-
def self.instantiate_element(container, klass, name, params)
|
501
|
-
container[presenter_class_name] ||= {}
|
502
|
-
container[presenter_class_name][name] = klass.new(name, params)
|
503
|
-
end
|
504
|
-
|
505
|
-
def self.scope(name, params = {})
|
506
|
-
self.instantiate_element(@@scopes, Carnival::Scope, name.to_sym, params)
|
507
|
-
end
|
508
|
-
|
509
|
-
def self.field(name, params = {})
|
510
|
-
self.instantiate_element(@@fields, Carnival::Field, name.to_sym, params)
|
511
|
-
end
|
512
|
-
|
513
|
-
def self.form(action, params = {})
|
514
|
-
self.instantiate_element(@@forms, Carnival::Form, name.to_sym, params)
|
515
|
-
end
|
516
|
-
|
517
|
-
def self.model_name(name)
|
518
|
-
@@model_names[presenter_class_name] = name
|
385
|
+
module_and_class = self.class.to_s.split('::')
|
386
|
+
module_and_class.size > 1 ? module_and_class.first : ''
|
519
387
|
end
|
520
388
|
|
521
|
-
def generate_route_path
|
389
|
+
def generate_route_path(params)
|
522
390
|
url_for(params) rescue nil
|
523
391
|
end
|
524
|
-
|
525
|
-
def self.presenter_class_name
|
526
|
-
if self.class.name == "Class"
|
527
|
-
self.to_s
|
528
|
-
else
|
529
|
-
self.class.name
|
530
|
-
end
|
531
|
-
end
|
532
|
-
|
533
|
-
def current_user
|
534
|
-
return @controller.current_user if @controller.respond_to?(:current_user)
|
535
|
-
nil
|
536
|
-
end
|
537
392
|
end
|
538
393
|
end
|