Module: CrudHelper

Defined in:
app/helpers/crud_helper.rb

Instance Method Summary collapse

Instance Method Details

#data(link) ⇒ Object



80
81
82
83
84
# File 'app/helpers/crud_helper.rb', line 80

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)


159
160
161
# File 'app/helpers/crud_helper.rb', line 159

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

#gen_icon(i) ⇒ Object



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

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

#imagem?(file) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'app/helpers/crud_helper.rb', line 151

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



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

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



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

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



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

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

#render_default_actions_crudObject



177
178
179
# File 'app/helpers/crud_helper.rb', line 177

def render_default_actions_crud
  render "default_actions_crud"
end

#render_field(field, f, modelo, record) ⇒ 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
142
143
144
145
146
147
148
149
# File 'app/helpers/crud_helper.rb', line 116

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
  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 
      unless modelo.reflect_on_association(field[:attribute])
         f.input field[:attribute], field[:sf]
      else 
         f.association field[:attribute], field[:sf]
      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
    unless modelo.reflect_on_association(field[:attribute])
      f.input field[:attribute], field[:sf]
    else 
      f.association field[:attribute], field[:sf]
    end 
  end
end

#render_field_file(field) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'app/helpers/crud_helper.rb', line 163

def render_field_file(field)
if imagem?(field) && field.url(:thumb)
  image_tag(field.url(:thumb))
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
# File 'app/helpers/crud_helper.rb', line 63

def render_link(link)
  if link[:partial].present?
    render link[:partial]
  else
    url = link[:url] || link[:link]
    link_to "#{gen_icon(link[:icon])} #{link[:text]}".html_safe, url, class: link[:class], data: data(link)
  end
end

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



111
112
113
114
# File 'app/helpers/crud_helper.rb', line 111

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

#shold_destroy?(crud_helper, record) ⇒ Boolean

Returns:

  • (Boolean)


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

def shold_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

#shold_edit?(crud_helper, record) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
191
192
# File 'app/helpers/crud_helper.rb', line 188

def shold_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

#shold_view?(crud_helper, record) ⇒ Boolean

Permissions

Returns:

  • (Boolean)


182
183
184
185
186
# File 'app/helpers/crud_helper.rb', line 182

def shold_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)


155
156
157
# File 'app/helpers/crud_helper.rb', line 155

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