Module: ResourceControl

Extended by:
ActiveSupport::Concern
Included in:
AbstractResourcesController
Defined in:
app/controllers/concerns/resource_control.rb

Instance Method Summary collapse

Instance Method Details

#_filter(r, options) ⇒ Object

filter - it at all



165
166
167
168
169
# File 'app/controllers/concerns/resource_control.rb', line 165

def _filter r, options
  return r if params[:f].blank?
  return resource_class.filter r, params[:f], options if resource_class.respond_to? :filter
  r
end

#_flip(val) ⇒ Object



184
185
186
187
188
189
# File 'app/controllers/concerns/resource_control.rb', line 184

def _flip val
  @sorting_direction = (val == "ASC" ? "DESC" : "ASC")
  val
rescue
  @sorting_direction = "ASC"
end

#_idObject



56
57
58
59
60
61
# File 'app/controllers/concerns/resource_control.rb', line 56

def _id
  return nil if !params[:id] || params[:id]=="0"
  params[:id] || params["#{resource_class.to_s.downcase}_id".to_sym]
rescue Exception => e
  scoop_from_error e
end

#_paginate(r) ⇒ Object

paginate - it at all



194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/concerns/resource_control.rb', line 194

def _paginate r
  return r if params[:action]=='print'
  # will it obey the #page call?
  # is it without format or html or scrolling or with search?
  if (r.respond_to?( :page)) && (params[:format].nil? or params[:format]=='html' or params[:scrolling] or params[:s])
    params[:perpage] ||= 20
    params[:page] ||= 1
    return r.page(params[:page]).per(params[:perpage])
  else
    return r
  end
end

#_search(options) ⇒ Object

search - it at all



154
155
156
157
158
159
160
# File 'app/controllers/concerns/resource_control.rb', line 154

def _search options
  if params[:q].blank? #or params[:q]=="undefined"
    parent? ? parent.send(resource_name) : (resource_class.nil? ? nil : find_all_resources(options))
  else
    find_resources_queried options
  end
end

#_sort(r, options) ⇒ Object

sort - it at all

%th{ role:“sort”, data{ field: “*barcode”, direction: “DESC”} } $(‘th’)



176
177
178
# File 'app/controllers/concerns/resource_control.rb', line 176

def _sort r, options
  r.order "%s %s" % [ (params[:s] || "id"), sorting_direction(params[:d]) ]
end

#find_all_resources(options) ⇒ Object

find queried resources collection - implement on each controller to customize raise an exception



210
211
212
# File 'app/controllers/concerns/resource_control.rb', line 210

def find_all_resources options
  policy_scope(resource_class)
end

#find_resources(options) ⇒ Object

find the resources collection



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/controllers/concerns/resource_control.rb', line 126

def find_resources options

  # return [] unless resource_class.ancestors.include? ActiveRecord::Base
  params[:ids] ||= []
  params[:ids] << params[:id] unless params[:id].nil?

  if params[:ids].compact.any?
    policy_scope(resource_class).where(id: params[:ids].compact.split(",").flatten)
  else
    # search
    r = _search options

    # filter
    r = _filter r, options

    # sort
    r = _sort r, options

    # paginate
    r = _paginate r

    # (params[:format].nil? or params[:format]=='html') ? r.page(params[:page]).per(params[:perpage]) : r
  end
end

#find_resources_queried(options) ⇒ Object

find queried resources collection - implement on each controller to customize raise an exception



217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/controllers/concerns/resource_control.rb', line 217

def find_resources_queried options
  case params[:f]
  when nil
    if parent?
      policy_scope(resource_class).tags_included?( params[:q].split(" ") ).where( options )
    else
      policy_scope(resource_class).tags_included?( params[:q].split(" ") )
    end
  else
    policy_scope(resource_class)
  end
end

#new_resourceObject

def resource=val

@resource=val

end



45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/concerns/resource_control.rb', line 45

def new_resource
  return nil if resource_class.nil?
  return resource_class.new if resource_class.ancestors.include?( ActiveRecord::Base ) and !(params.include? resource_class.to_s.underscore) #[ 'create', 'update' ].include? params[:action]
  p = resource_params
  p=p.compact.first if p.class==Array
  return resource_class.new(p.merge(current_user: current_user)) if resource_class.ancestors.include? ActiveRecord::Base
  nil
rescue Exception => e
  scoop_from_error e
end

#resourceObject



32
33
34
35
36
37
38
39
# File 'app/controllers/concerns/resource_control.rb', line 32

def resource
  @resource ||= (_id.nil? ? new_resource : resource_class.find(_id) )
  return @resource if @resource.nil?
  @resource.current_user = (current_user || nil) if @resource.respond_to? :current_user
  @resource
rescue Exception => e
  scoop_from_error e
end

#resource?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'app/controllers/concerns/resource_control.rb', line 26

def resource?
  !(%w{NilClass TrueClass FalseClass}.include? @resource.class.to_s)
rescue Exception => e
  scoop_from_error e
end

#resource_classObject



70
71
72
73
74
75
# File 'app/controllers/concerns/resource_control.rb', line 70

def resource_class
  return @resource_class if resource?
  @resource_class ||= params[:controller].singularize.classify.constantize
rescue Exception => e
  scoop_from_error e
end

#resource_class=(val) ⇒ Object



77
78
79
# File 'app/controllers/concerns/resource_control.rb', line 77

def resource_class= val
  @resource_class = val
end

#resource_name(options = {}) ⇒ Object



63
64
65
66
67
68
# File 'app/controllers/concerns/resource_control.rb', line 63

def resource_name options={}
  resource_class.table_name
rescue Exception => e
  scoop_from_error e
  # resource_class.to_s.underscore.pluralize
end

#resource_options(options = {}) ⇒ Object



119
120
121
122
# File 'app/controllers/concerns/resource_control.rb', line 119

def resource_options options={}
  options.merge! params.except( "id", "controller", "action", "utf8", "_method", "authenticity_token" )
  options.empty? ? "" : "?" + options.collect{ |k,v| "#{k}=#{v}" }.join("&")
end

#resource_paramsObject



13
14
15
# File 'app/controllers/concerns/resource_control.rb', line 13

def resource_params
  raise 'You need to "def resource_params" on the %sController! (see: http://blog.trackets.com/2013/08/17/strong-parameters-by-example.html)' % params[:controller].capitalize
end

#resource_url(options = {}) ⇒ Object

returns the url for the resource - like /users/1



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/concerns/resource_control.rb', line 91

def resource_url options={}
  r=request.path
  id=params[:id]
  options = case params[:action]
    when 'create','update','delete','destroy'; ""
    else resource_options(options)
  end
  return "%s%s" % [r,options] if r.match "#{resource_class.to_s.tableize}\/#{id}$"
  "%s%s" % [ r.split("/#{params[:action]}")[0], options]
rescue Exception => e
  scoop_from_error e
end

#resources(options = {}) ⇒ Object

return the resources collection - preferably from the cache



84
85
86
# File 'app/controllers/concerns/resource_control.rb', line 84

def resources options={}
  @resources ||= find_resources options
end

#resources_url(options = {}) ⇒ Object

returns the url for the resources - /employees or /employees/1/events



107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/concerns/resource_control.rb', line 107

def resources_url options={}
  r=request.path
  options = case params[:action]
    when 'create','update','delete','destroy'; ""
    else resource_options(options)
  end
  return "%s%s" % [r,options] if r.match "#{resource_class.to_s.tableize}$"
  "%s%s%s" % [ r.split( resource_class.to_s.tableize)[0],resource_class.to_s.tableize, options]
rescue Exception => e
  scoop_from_error e
end

#set_resourceObject



17
18
19
20
# File 'app/controllers/concerns/resource_control.rb', line 17

def set_resource
  parent
  resource
end

#set_resourcesObject



22
23
24
# File 'app/controllers/concerns/resource_control.rb', line 22

def set_resources
  resources
end

#sorting_direction(val = nil) ⇒ Object



180
181
182
# File 'app/controllers/concerns/resource_control.rb', line 180

def sorting_direction val=nil
  _flip(val || @sorting_direction)
end