Module: Carnival::BaseAdminHelper
- Defined in:
- app/helpers/carnival/base_admin_helper.rb
Instance Method Summary collapse
- #button_action(action, presenter, record) ⇒ Object
- #button_action_remote(action, presenter, record) ⇒ Object
- #caminho_modelo(action, extra_params = nil) ⇒ Object
- #carnival_render_if_exist(partial_path) ⇒ Object
- #empty_cel(presenter, field, record, only_render_fields) ⇒ Object
- #field_to_show(presenter, field_name, record, show_only_values = false) ⇒ Object
- #field_value_and_type(presenter, field_name, record) ⇒ Object
- #format_field(presenter, field_name, field_type, value) ⇒ Object
- #get_css_class(presenter, field, record) ⇒ Object
- #get_partial_path(partial_path) ⇒ Object
- #grid_attr(presenter, field, record, only_render_fields) ⇒ Object
- #has_many_relation?(model, field) ⇒ Boolean
- #has_one_relation?(model, field) ⇒ Boolean
- #hide_column?(field, records, controller) ⇒ Boolean
- #is_image?(field_type, value) ⇒ Boolean
- #list_attr(presenter, field, record, only_render_fields) ⇒ Object
- #list_buttons(presenter, record) ⇒ Object
- #list_cel(presenter, field, record, only_render_fields) ⇒ Object
- #many_to_many_relation?(model, field) ⇒ Boolean
- #menu_link(link) ⇒ Object
- #partial_exist?(partial_path) ⇒ Boolean
- #set_resource_actived_if_current(path) ⇒ Object
- #show_as_list(presenter, field) ⇒ Object
- #show_messages ⇒ Object
- #show_view(presenter, field) ⇒ Object
- #translate_field(presenter, field_name) ⇒ Object
Instance Method Details
#button_action(action, presenter, record) ⇒ Object
210 211 212 213 214 215 216 217 218 219 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 210 def (action, presenter, record) label = t("#{presenter.model_name.underscore}.#{action.name}", default: t("carnival.#{action.name}")) path = action.path(presenter, id: record.id) if action.default_partial == :default link_to label, path, class: "carnival-action-link #{label.parameterize}", data: action.data elsif action.default_partial == :delete link_to label, path, class: 'carnival-action-link apagar', method: :delete, data: { confirm: I18n.t('are_you_sure') } end end |
#button_action_remote(action, presenter, record) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 221 def (action, presenter, record) params = action.params label = t("#{presenter.model_name}.#{action.name}", default: t("carnival.#{action.name}")) path = action.path(presenter, id: record.id) success_callback = params[:success] || "#{action.name}_success_callback" error_callback = params[:error] || "#{action.name}_error_callback" params[:show_overlay] = params[:show_overlay].presence || false remote_function = "Carnival.remoteFunction('#{path}', '#{success_callback}', '#{error_callback}', '#{params[:method]}', {} , #{params[:show_overlay]})" link_to label, "javascript: #{remote_function}", class: 'carnival-action-link editar' end |
#caminho_modelo(action, extra_params = nil) ⇒ Object
21 22 23 24 25 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 21 def caminho_modelo(action, extra_params = nil) params = { controller: controller.controller_name, action: action } params = params.merge(extra_params) if extra_params.present? url_for(params) end |
#carnival_render_if_exist(partial_path) ⇒ Object
27 28 29 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 27 def carnival_render_if_exist(partial_path) render partial_path if partial_exist?(partial_path) end |
#empty_cel(presenter, field, record, only_render_fields) ⇒ Object
157 158 159 160 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 157 def empty_cel(presenter, field, record, only_render_fields) return "<td class='first-td'><span class='#{get_css_class(presenter, field, record)}'></span></td>" if presenter.get_field(field).css_class.present? "<td class='first-td'></td>" end |
#field_to_show(presenter, field_name, record, show_only_values = false) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 87 def field_to_show(presenter, field_name, record, show_only_values=false) if presenter.fields[field_name].params[:as] == :partial raw(render presenter.fields[field_name].partial_name, record: record) else rendered = field_value_and_type presenter, field_name, record field_type = rendered[:field_type] value = rendered[:value] is_relation = presenter.relation_field?(field_name) if value.present? formatted_field = format_field(presenter, field_name, field_type, value) if is_relation && !show_only_values link_to(formatted_field, presenter.relation_path(field_name, record)) else formatted_field end end end end |
#field_value_and_type(presenter, field_name, record) ⇒ Object
108 109 110 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 108 def field_value_and_type(presenter, field_name, record) presenter.render_field(field_name, record) end |
#format_field(presenter, field_name, field_type, value) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 116 def format_field(presenter, field_name, field_type, value) if is_image?(field_type, value) image_tag value.url(:thumb), alt: translate_field(presenter, field_name), class: 'attr' else case field_type when :datetime begin I18n.l(value, format: :long_date) rescue I18n::MissingTranslationData value.strftime('%d/%m/%y %H:%M:%S') end when :date begin I18n.l(value, format: :short_date) rescue I18n::MissingTranslationData value.strftime('%d/%m/%Y') end when :number number_with_precision(value, precision: 2, separator: ',') when :enum presenter.model_class.const_get(field_name.upcase)[value] else value end end end |
#get_css_class(presenter, field, record) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 185 def get_css_class(presenter, field, record) css_class = presenter.get_field(field).css_class if css_class.is_a? Hash record.send(css_class[:method]) else css_class css_class.presence || '' end end |
#get_partial_path(partial_path) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 50 def get_partial_path(partial_path) path = Rails.root.join('app', 'views') partial_path_array = partial_path.split('/') partial_path_array.each do |pp| if pp == partial_path_array.last path = path.join "_#{pp}.html.haml" else path = path.join pp end end path end |
#grid_attr(presenter, field, record, only_render_fields) ⇒ Object
162 163 164 165 166 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 162 def grid_attr(presenter, field, record, only_render_fields) result = field_to_show(presenter, field, record, only_render_fields) return result if only_render_fields list_attr(presenter, field, record, only_render_fields) end |
#has_many_relation?(model, field) ⇒ Boolean
35 36 37 38 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 35 def has_many_relation?(model, field) klass = Carnival::KlassService.new model.class klass.is_a_has_many_relation?(field.to_sym) end |
#has_one_relation?(model, field) ⇒ Boolean
40 41 42 43 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 40 def has_one_relation?(model, field) klass = Carnival::KlassService.new model.class klass.is_a_has_one_relation?(field.to_sym) end |
#hide_column?(field, records, controller) ⇒ Boolean
168 169 170 171 172 173 174 175 176 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 168 def hide_column?(field, records, controller) hide = true records.each do |record| unless field.hidden?(record, controller) hide = false end end hide end |
#is_image?(field_type, value) ⇒ Boolean
112 113 114 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 112 def is_image?(field_type, value) field_type.nil? and value.class.to_s == "Paperclip::Attachment" and value.content_type.include? "image" end |
#list_attr(presenter, field, record, only_render_fields) ⇒ Object
178 179 180 181 182 183 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 178 def list_attr(presenter, field, record, only_render_fields) result = field_to_show(presenter, field, record, only_render_fields) return result if only_render_fields return "<div class='attr'><div class='label #{field}'>#{translate_field(presenter, field)}:</div><div class='field_value #{field} #{get_css_class(presenter, field, record)}'>#{result}</div></div>" if presenter.get_field(field).css_class.present? "<div class='attr'><div class='label #{field}'>#{translate_field(presenter, field)}:</div><div class='field_value #{field}'>#{result}</div></div>" end |
#list_buttons(presenter, record) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 194 def (presenter, record) if presenter.actions_for_record.any? presenter.actions_for_record.map do |_, record_action| next unless presenter.render_action?(record, record_action, params[:action]) next if record_action.hidden?(record, controller) if record_action.remote? (record_action, presenter, record) else (record_action, presenter, record) end end.reject(&:nil?).inject(:+) else "" end end |
#list_cel(presenter, field, record, only_render_fields) ⇒ Object
149 150 151 152 153 154 155 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 149 def list_cel(presenter, field, record, only_render_fields) result = field_to_show(presenter, field, record, only_render_fields) return result if only_render_fields td = "<td class='first-td'>" return "#{td}<span class='#{get_css_class(presenter, field, record)}'>#{result}</span></td>" if presenter.get_field(field).css_class.present? "#{td}#{result}</td>" end |
#many_to_many_relation?(model, field) ⇒ Boolean
45 46 47 48 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 45 def many_to_many_relation?(model, field) klass = Carnival::KlassService.new model.class klass.is_a_many_to_many_relation?(field.to_sym) end |
#menu_link(link) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 63 def (link) link.strip! if link.to_s.end_with?('_path') || link.to_s.end_with?('_url') return eval link elsif link.index(/_path.+/) #path with arguments return eval link end link end |
#partial_exist?(partial_path) ⇒ Boolean
31 32 33 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 31 def partial_exist?(partial_path) File.exist?(get_partial_path(partial_path)) end |
#set_resource_actived_if_current(path) ⇒ Object
3 4 5 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 3 def set_resource_actived_if_current(path) (request.fullpath == path) ? 'actived' : '' end |
#show_as_list(presenter, field) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 78 def show_as_list(presenter, field) current_type = presenter.field_type(field) if current_type != :relation false else presenter.get_field(field).show_as_list end end |
#show_messages ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 7 def @messages = '' flash.each do |type, | @messages = @type = type end javascript = "noty({'text':'#{@messages}', 'layout':'bottom','type':'#{@type}', 'animation': {'close':{'height':'toggle'}, 'open':{'height':'toggle'},'speed':500},'timeout':4000, 'closeWith':['click', 'hover']});" return javascript if defined?(@type) '' end |
#show_view(presenter, field) ⇒ Object
74 75 76 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 74 def show_view(presenter, field) presenter.get_field(field).show_view end |
#translate_field(presenter, field_name) ⇒ Object
143 144 145 146 147 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 143 def translate_field(presenter, field_name) field = presenter.get_field(field_name) field_key = field.name_for_translation presenter.model_class.human_attribute_name field_key end |