Module: CrudHelper

Defined in:
app/helpers/crud_helper.rb

Instance Method Summary collapse

Instance Method Details

#can_print_pdf?(crud_helper, record) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
234
235
# File 'app/helpers/crud_helper.rb', line 231

def can_print_pdf?(crud_helper, record)
  return false unless can?(:read, record)
  return true if crud_helper.condition_printing_action.nil?
  crud_helper.condition_printing_action.call(record)
end

#data(link) ⇒ Object



82
83
84
85
86
# File 'app/helpers/crud_helper.rb', line 82

def data(link)
  data = {}
  link[:data].each_key{|k| data[k] = link[:data][k]} if link[:data]
  return data
end

#documento?(file) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'app/helpers/crud_helper.rb', line 172

def documento?(file)
  !(video?(file) || imagem?(file))
end

#gen_icon(i) ⇒ Object



74
75
76
77
78
79
80
# File 'app/helpers/crud_helper.rb', line 74

def gen_icon(i)
  if i
    "<i class='#{i}'></i>"
  else
    ""
  end
end

#imagem?(file) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'app/helpers/crud_helper.rb', line 164

def imagem?(file)
  file.present? && file.content_type.start_with?('image')
end

#is_action_create?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/helpers/crud_helper.rb', line 42

def is_action_create?
  params[:action] == "create"
end

#is_action_edit?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/helpers/crud_helper.rb', line 38

def is_action_edit?
  params[:action] == "edit"
end

#is_action_index?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/helpers/crud_helper.rb', line 54

def is_action_index?
  params[:action] == "index"
end

#is_action_new?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/helpers/crud_helper.rb', line 50

def is_action_new?
  params[:action] == "new"
end

#is_action_query?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/helpers/crud_helper.rb', line 58

def is_action_query?
  params[:action] == "query"
end

#is_action_show?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/helpers/crud_helper.rb', line 46

def is_action_show?
  params[:action] == "show"
end

#is_active_action(action_name) ⇒ Object



7
8
9
# File 'app/helpers/crud_helper.rb', line 7

def is_active_action(action_name)
  params[:action] == action_name ? "active" : nil
end

#is_active_controller(controller_name) ⇒ Object



3
4
5
# File 'app/helpers/crud_helper.rb', line 3

def is_active_controller(controller_name)
  params[:controller] == controller_name ? "active" : nil
end

#is_raro_crud(classe) ⇒ Object



11
12
13
14
15
16
17
18
# File 'app/helpers/crud_helper.rb', line 11

def is_raro_crud(classe)
  #if(Rails.env == "production")
  #  @@cruds = Dir[Rails.root.join("app","raro_crud","*.rb")].map{|f| f.split(/\//).last.gsub(/_crud\.rb/,'')} unless @@cruds
  #else
    @@cruds = Dir[Rails.root.join("app","raro_crud","*.rb")].map{|f| f.split(/\//).last.gsub(/_crud\.rb/,'')}
  #end
  return @@cruds.include?(classe.underscore.to_s)
end

#lista_menus_crud(raro_models) ⇒ Object



20
21
22
23
24
25
26
# File 'app/helpers/crud_helper.rb', line 20

def lista_menus_crud(raro_models)
  menus = []
  raro_models.each do |modelo|
    menus << ['crud', "#{modelo.name.underscore}"]
  end
  menus
end


28
29
30
31
32
33
34
35
36
# File 'app/helpers/crud_helper.rb', line 28

def menu_helper_crud(modelo, url, nome, classe, icon='')
  if can?(:read, classe)
    buffer = ""
    buffer << "<li class='childreen #{controller.controller_name == 'crud' and params[:model] == modelo ? 'active' : '' }'>"
    buffer << link_to("<i class='#{icon}'></i><span>#{nome}</span>".html_safe, url, data: {push: true, crumb: 'wielka'})
    buffer << "</li>"
    buffer.html_safe
  end
end

#raro_modelsObject



88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/crud_helper.rb', line 88

def raro_models
  models = []
  Dir["#{Rails.root.to_s}/app/raro_crud/*"].each do |f|
    m = f.split("/").last.split("_crud").first.camelize
    if m != "Raro"
      models << m.constantize
    end
  end
  models.uniq.flatten
end

#raro_permissionsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/crud_helper.rb', line 99

def raro_permissions
  permissions = []
  Dir["#{Rails.root.to_s}/app/controllers/*"].each do |f|
    permission = f.split("/").last.split(".").first.camelize.constantize.instance_variable_get("@permissao")
    if permission.present?
      permissions << permission
    end
  end
  raro_models.each do |m|
    permissions << m.name
  end
  permissions.uniq.flatten
end

#render_crud(&block) ⇒ Object



192
193
194
# File 'app/helpers/crud_helper.rb', line 192

def render_crud(&block)
  render "/crud/shared", block: block
end

#render_default_actions_crudObject



196
197
198
# File 'app/helpers/crud_helper.rb', line 196

def render_default_actions_crud
  render "default_actions_crud"
end

#render_field(field, f, modelo, record) ⇒ Object



118
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/helpers/crud_helper.rb', line 118

def render_field(field,f,modelo,record)
  field[:sf][:wrapper] ||= :default
  if field[:sf].present? && field[:sf][:if].present?
    return unless field[:sf][:if].call(f.object)
  end
  field[:sf][:hint] = false if field[:sf].present? && !field[:sf][:hint].present?
  if field[:sf].present? && field[:sf][:date_format].present? && f.object.send(field[:attribute]).present? && Date <= modelo.columns_hash[field[:attribute].to_s].type.to_s.camelcase.constantize
    field[:sf][:input_html] ||= {}
    field[:sf][:input_html][:value] = f.object.send(field[:attribute]).strftime(field[:sf][:date_format])
  end
  if !field[:sf][:edit].nil? || !field[:sf][:create].nil?
    if !field[:sf][:edit].nil? && !field[:sf][:edit] && !record.new_record?
    elsif !field[:sf][:create].nil? && !field[:sf][:create] && record.new_record?
    else
      opts = field[:sf].merge(label: I18n.t(field[:sf][:label], default: I18n.t("shared.#{field[:attribute]}")))
      unless modelo.reflect_on_association(field[:attribute])
        if modelo.new.send(field[:attribute]).class.to_s =~ /Uploader/ and f.object.send(field[:attribute]).present?
          f.input field[:attribute], opts.merge(hint: "Arquivo Atual: #{f.object.send(field[:attribute]).file.filename}")
        else
          f.input field[:attribute], opts
        end
      else
         f.association field[:attribute], opts
      end
    end
  else
    if field[:sf][:value] and field[:sf][:value].class == Proc
       field[:sf][:input_html] ||= {}
       field[:sf][:input_html][:value] = f.instance_eval(&field[:sf][:value])
    end
    if field[:sf][:collection_if] and field[:sf][:collection_if].class == Proc
       field[:sf][:collection] = f.instance_eval(&field[:sf][:collection_if])
    end
    opts = field[:sf].merge(label: I18n.t(field[:sf][:label], default: I18n.t("shared.#{field[:attribute]}")))
    unless modelo.reflect_on_association(field[:attribute])
      if modelo.new.send(field[:attribute]).class.to_s =~ /Uploader/ and f.object.send(field[:attribute]).present?
        f.input field[:attribute], opts.merge(hint: "Arquivo Atual: #{f.object.send(field[:attribute]).file.filename}")
      else
        f.input field[:attribute], opts
      end
    else
      f.association field[:attribute], opts
    end
  end
end

#render_field_file(field) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/helpers/crud_helper.rb', line 176

def render_field_file(field)
if imagem?(field) && field.respond_to?(:thumb)
    if is_active_action("printing")
      pdf_image_tag(field, width: '100px')
    else
      link_to field.url, target: "_blank" do
        image_tag(field.url(:thumb), width: '100px')
      end
    end
elsif video?(field)
	link_to field, field.url, target: "_blank"
else
	link_to field, field.url, target: "_blank"
end
end


63
64
65
66
67
68
69
70
71
72
# File 'app/helpers/crud_helper.rb', line 63

def render_link(link,url)
  nome_modelo = I18n.t("model.#{link[:modelo].underscore}")
  if link[:partial].present?
    render link[:partial]
  elsif link[:link].present?
    link_to "#{gen_icon(link[:icon])} #{I18n.t(link[:text], model: nome_modelo)}".html_safe, "#{url}/#{link[:link]}", class: link[:class], data: data(link)
  else
    link_to "#{gen_icon(link[:icon])} #{I18n.t(link[:text], model: nome_modelo)}".html_safe, link[:url], class: link[:class], data: data(link)
  end
end

#render_plus_button(field, f, modelo, record) ⇒ Object



113
114
115
116
# File 'app/helpers/crud_helper.rb', line 113

def render_plus_button(field,f,modelo,record)
  field[:sf][:wrapper] = :with_button
  render_field(field,f,modelo,record)
end

#should_destroy?(crud_helper, record) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
# File 'app/helpers/crud_helper.rb', line 213

def should_destroy?(crud_helper, record)
  return false unless can?(:destroy, record)
  return true if crud_helper.condition_destroy_action.nil?
  crud_helper.condition_destroy_action.call(record)
end

#should_edit?(crud_helper, record) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
210
211
# File 'app/helpers/crud_helper.rb', line 207

def should_edit?(crud_helper, record)
  return false unless can?(:update, record)
  return true if crud_helper.condition_edit_action.nil?
  crud_helper.condition_edit_action.call(record)
end

#should_listing?(crud_helper, model) ⇒ Boolean

Returns:

  • (Boolean)


219
220
221
222
223
# File 'app/helpers/crud_helper.rb', line 219

def should_listing?(crud_helper, model)
  return false unless can?(:read, model)
  return true if crud_helper.condition_listing_action.nil?
  crud_helper.condition_listing_action.call(model)
end

#should_listing_excel?(crud_helper, model) ⇒ Boolean

Returns:

  • (Boolean)


225
226
227
228
229
# File 'app/helpers/crud_helper.rb', line 225

def should_listing_excel?(crud_helper, model)
  return false unless can?(:read, model)
  return true if crud_helper.condition_listing_excel.nil?
  crud_helper.condition_listing_excel.call(model)
end

#should_view?(crud_helper, record) ⇒ Boolean

Permissions

Returns:

  • (Boolean)


201
202
203
204
205
# File 'app/helpers/crud_helper.rb', line 201

def should_view?(crud_helper, record)
  return false unless can?(:read, record)
  return true if crud_helper.condition_view_action.nil?
  crud_helper.condition_view_action.call(record)
end

#video?(file) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'app/helpers/crud_helper.rb', line 168

def video?(file)
  file.present? && file.content_type.start_with?('video')
end