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
- #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
- #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
198 199 200 201 202 203 204 205 206 207 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 198 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}" 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
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 209 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 |
#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 field_name.to_s, 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 111 112 113 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 108 def field_value_and_type(presenter, field_name, record) renderer = FieldRenderers::RendererCreator .create_field_renderer(presenter, field_name) renderer.render_field(record) end |
#format_field(presenter, field_name, field_type, value) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 119 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
173 174 175 176 177 178 179 180 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 173 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
160 161 162 163 164 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 160 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 |
#is_image?(field_type, value) ⇒ Boolean
115 116 117 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 115 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
166 167 168 169 170 171 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 166 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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 182 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 unless record_action.show(record) 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
152 153 154 155 156 157 158 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 152 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
146 147 148 149 150 |
# File 'app/helpers/carnival/base_admin_helper.rb', line 146 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 |