Module: Carnival::BaseAdminHelper

Defined in:
app/helpers/carnival/base_admin_helper.rb

Instance Method Summary collapse

Instance Method Details

#button_action(action, presenter, record) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'app/helpers/carnival/base_admin_helper.rb', line 201

def button_action(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
    "<a class='carnival-action-link #{label.parameterize}' href='#{path}'>#{label}</a>"
  elsif action.default_partial == :delete
    confirm = I18n.t("are_you_sure")
    "<a class='carnival-action-link apagar' data-confirm='#{confirm}' data-method='delete' href='#{path}' rel='nofollow'>#{label}</a>"
  end
end

#button_action_remote(action, presenter, record) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/helpers/carnival/base_admin_helper.rb', line 212

def button_action_remote(action, presenter, record)
  name = action.name
  params = action.params
  label =  t("#{presenter.model_name}.#{action.name}", default: t("carnival.#{action.name}"))
  path = action.path(presenter, :id => record.id)

  success_callback = "#{name}_success_callback"
  if params[:success]
    success_callback = params[:success]
  end

  error_callback = "#{name}_error_callback"
  if params[:error]
    error_callback = params[:error]
  end

  data_overlay = ''
  if params[:show_overlay]
    data_overlay = 'data-carnival-show-overlay=true'
  end

  params[:show_overlay] = false if params[:show_overlay].nil?

  remote_function = "Carnival.remoteFunction(\"#{path}\", \"#{success_callback}\", \"#{error_callback}\", \"#{params[:method]}\", {} ,#{params[:show_overlay]})"

  "<a class='carnival-action-link editar' href='javascript:#{remote_function}'>#{label}</a>"
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
30
31
# File 'app/helpers/carnival/base_admin_helper.rb', line 27

def carnival_render_if_exist partial_path
  if partial_exist?(partial_path)
    return render partial_path
  end
end

#field_to_show(presenter, field_name, record, show_only_values = false) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/carnival/base_admin_helper.rb', line 89

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)

    unless value.nil?
      formatted_field = format_field(presenter, field_name, field_type, value)
      if is_relation and !show_only_values
        link_to(formatted_field, presenter.relation_path(field_name, record))
      else
        formatted_field
      end
    else
      nil
    end
  end
end

#field_value_and_type(presenter, field_name, record) ⇒ Object



112
113
114
115
116
117
# File 'app/helpers/carnival/base_admin_helper.rb', line 112

def field_value_and_type presenter, field_name, record
  renderer = FieldRenderers::RendererCreator
    .create_field_renderer(presenter, field_name)

  rendered = renderer.render_field(record)
end

#format_field(presenter, field_name, field_type, value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/helpers/carnival/base_admin_helper.rb', line 123

def format_field(presenter, field_name, field_type, value)
  if is_image?(field_type, value)
    "<img class='attr' src='#{value.url(:thumb)}' alt='#{translate_field(presenter, field_name)}' />"
  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



177
178
179
180
181
182
183
# File 'app/helpers/carnival/base_admin_helper.rb', line 177

def get_css_class presenter, field, record
  css_class = presenter.get_field(field).css_class
  return '' if !css_class
  return record.send(css_class[:method]) if css_class.is_a? Hash
  return css_class if css_class.is_a? String
  return ''
end

#get_partial_path(partial_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/carnival/base_admin_helper.rb', line 52

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



164
165
166
167
168
# File 'app/helpers/carnival/base_admin_helper.rb', line 164

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

Returns:

  • (Boolean)


37
38
39
40
# File 'app/helpers/carnival/base_admin_helper.rb', line 37

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

Returns:

  • (Boolean)


42
43
44
45
# File 'app/helpers/carnival/base_admin_helper.rb', line 42

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

Returns:

  • (Boolean)


119
120
121
# File 'app/helpers/carnival/base_admin_helper.rb', line 119

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



170
171
172
173
174
175
# File 'app/helpers/carnival/base_admin_helper.rb', line 170

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/helpers/carnival/base_admin_helper.rb', line 185

def list_buttons(presenter, record)
  result = ""
  presenter.actions_for_record.each do |key, record_action|
    if presenter.render_action?(record, record_action, params[:action])
      if record_action.show(record)
        if record_action.remote?
          result << button_action_remote(record_action, presenter, record)
        else
          result << button_action(record_action, presenter, record)
        end
      end
    end
  end
  result
end

#list_cel(presenter, field, record, only_render_fields) ⇒ Object



156
157
158
159
160
161
162
# File 'app/helpers/carnival/base_admin_helper.rb', line 156

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

Returns:

  • (Boolean)


47
48
49
50
# File 'app/helpers/carnival/base_admin_helper.rb', line 47

def many_to_many_relation? model, field
  klass = Carnival::KlassService.new model.class
  klass.is_a_many_to_many_relation?(field.to_sym)
end


65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/carnival/base_admin_helper.rb', line 65

def menu_link link
  link.strip!
  if link.to_s.end_with?('_path') or 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

Returns:

  • (Boolean)


33
34
35
# File 'app/helpers/carnival/base_admin_helper.rb', line 33

def partial_exist? partial_path
  File.exists?(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



80
81
82
83
84
85
86
87
# File 'app/helpers/carnival/base_admin_helper.rb', line 80

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_messagesObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/helpers/carnival/base_admin_helper.rb', line 7

def show_messages
  @messages = ''
  flash.each do |type, message|
    @messages = message
    @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



76
77
78
# File 'app/helpers/carnival/base_admin_helper.rb', line 76

def show_view(presenter, field)
  presenter.get_field(field).show_view
end

#translate_field(presenter, field_name) ⇒ Object



150
151
152
153
154
# File 'app/helpers/carnival/base_admin_helper.rb', line 150

def translate_field(presenter, field_name)
  field = presenter.get_field(field_name)
  field_key = field.name_for_translation
  presenter.full_model_name.classify.constantize.human_attribute_name field_key
end