Module: QM::ActsAsGenericController::ModelIncludes::ClassMethods

Defined in:
lib/qm-acts-as-generic-controller-model.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(association_id, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/qm-acts-as-generic-controller-model.rb', line 112

def belongs_to(association_id, options = {})
  @generic_field_associations ||= {}
  unless options.has_key?(:polymorphic)
    klass  = ( options.has_key?(:class_name) ? options[:class_name].to_s.constantize : association_id.to_s.classify.constantize )

    @generic_field_associations[association_id.to_sym] = { :kind => :belongs_to, :class_name => klass, :foreign_key => options[:foreign_key] || "#{klass.to_s.tableize}_id" } 
  end

  super association_id, options  
end

#generic_actions(options = {}) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/qm-acts-as-generic-controller-model.rb', line 148

def generic_actions(options = {})
  @generic_actions ||= {}  
  if options.has_key? :action
    @generic_actions[options[:action]] || []
  else
    @generic_actions || []
  end
end

#generic_field_associationsObject



90
91
92
# File 'lib/qm-acts-as-generic-controller-model.rb', line 90

def generic_field_associations
  @generic_field_associations || {}
end

#generic_fields(options = {}) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/qm-acts-as-generic-controller-model.rb', line 157

def generic_fields(options = {})
  @generic_fields ||= {}  
  if options.has_key? :action
    @generic_fields[options[:action]] || []
  else
    @generic_fields || []
  end
end

#generic_form_fieldsetsObject



166
167
168
# File 'lib/qm-acts-as-generic-controller-model.rb', line 166

def generic_form_fieldsets
  @generic_form_fieldsets
end

#generic_named_scopesObject



127
128
129
130
131
# File 'lib/qm-acts-as-generic-controller-model.rb', line 127

def generic_named_scopes
  scopes = {}
  named_scopes.each{ |i, o| scopes[i] = o if o.is_a? Hash and o.has_key?(:generic) and o[:generic] } # TODO creating generic named_scopes with lambdas
  scopes
end

#has_and_belongs_to_many(association_id, options = {}, &extension) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/qm-acts-as-generic-controller-model.rb', line 103

def has_and_belongs_to_many(association_id, options = {}, &extension)
  @generic_field_associations ||= {}
  @generic_field_associations[association_id.to_sym] = { :kind => :has_and_belongs_to_many, :class_name => ( options.has_key?(:class_name) ? options[:class_name].constantize : association_id.to_s.classify.constantize ), :readonly => options[:readonly] || false, :through => options[:through].present?, :generic_create => options[:generic_create] || false }

  options.delete :generic_create
  
  super association_id, options, &extension
end

#has_generic_action_for(actions, name, options = {}) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/qm-acts-as-generic-controller-model.rb', line 187

def has_generic_action_for(actions, name, options = {})
  options = options.symbolize_keys!
  @generic_actions ||= {}
  
  if actions.is_a? Array
    actions.each do |action|
      raise ArgumentError, "action must be one of :index, :show, :form" unless [ :index, :show, :form ].include? action.to_sym
      @generic_actions[action.to_sym] ||= []
      @generic_actions[action.to_sym] << { :name => name.to_sym, :options => options }
    end
  else
    raise ArgumentError, "action must be one of :index, :show, :form" unless [ :index, :show, :form ].include? actions.to_sym
    @generic_actions[actions.to_sym] ||= []
    @generic_actions[actions.to_sym] << { :name => name.to_sym, :options => options }
  end

end

#has_generic_field_for(actions, name, options = {}) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/qm-acts-as-generic-controller-model.rb', line 170

def has_generic_field_for(actions, name, options = {})
  options = options.symbolize_keys!
  @generic_fields ||= {}
  
  if actions.is_a? Array
    actions.each do |action|
      raise ArgumentError, "action must be one of :index, :show, :form" unless [ :index, :show, :form ].include? action.to_sym
      @generic_fields[action.to_sym] ||= []
      @generic_fields[action.to_sym] << { :name => name.to_sym, :options => options }
    end
  else
    raise ArgumentError, "action must be one of :index, :show, :form" unless [ :index, :show, :form ].include? actions.to_sym
    @generic_fields[actions.to_sym] ||= []
    @generic_fields[actions.to_sym] << { :name => name.to_sym, :options => options }
  end
end

#has_generic_form_fieldsets(*fieldsets) ⇒ Object



205
206
207
# File 'lib/qm-acts-as-generic-controller-model.rb', line 205

def has_generic_form_fieldsets(*fieldsets)
  @generic_form_fieldsets = fieldsets.flatten
end

#has_generic_named_scopes?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/qm-acts-as-generic-controller-model.rb', line 133

def has_generic_named_scopes?
  generic_named_scopes.size != 0
end

#has_many(association_id, options = {}, &extension) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/qm-acts-as-generic-controller-model.rb', line 94

def has_many(association_id, options = {}, &extension)
  @generic_field_associations ||= {}
  @generic_field_associations[association_id.to_sym] = { :kind => :has_many, :class_name => ( options.has_key?(:class_name) ? options[:class_name].constantize : association_id.to_s.classify.constantize ), :readonly => options[:readonly] || false, :through => options[:through].present?, :generic_create => options[:generic_create] || false }
  
  options.delete :generic_create
  
  super association_id, options, &extension
end

#limit_with_security_scheme(options) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/qm-acts-as-generic-controller-model.rb', line 9

def limit_with_security_scheme(options)
  # options[:user] - current user that performs the query
  # options[:paginate] - set to true if you want to enable pagination, not applicable for the ExternalSystem request
  # options[:paginate_page] - set to integer
  # options[:sort_by] - field to sort by
  # options[:sort_order] - "A" or "D"
  
  raise ArgumentError, "You must pass options[:user] to limit_with_security_scheme" unless options.has_key? :user

  return all if options[:user].is_a? ExternalSystem

  # Return empty set if user has no privileges at all (nor index_any and index_created)
  unless options[:user].has_privileges?(:class_name => self, :generic_action => :index_any) or options[:user].has_privileges?(:class_name => self, :generic_action => :index_created)
    return []
  end
  
  creator_column = "creator_#{options[:user].class.to_s.tableize.singularize}_id"
  should_limit_index_for_created = columns_hash.has_key?(creator_column) && options[:user].respond_to?(:has_privileges?) && !options[:user].has_privileges?(:class_name => self, :generic_action => :index_any) && options[:user].has_privileges?(:class_name => self, :generic_action => :index_created)
  
  universal_find_params = {}
  
  if options.has_key? :sort_by and not options[:sort_by].nil? and not options[:sort_by].to_s.empty?
    # Sorting
    if options[:sort_order] == "A"
      sort_direction = "ASC"
      sort_opposite_direction = "DESC"
    else
      sort_direction = "DESC"
      sort_opposite_direction = "ASC"
    end
    
    # FIXME what if this is not index?
    generic_field = self.generic_fields[:index].detect{ |f| f[:name] == options[:sort_by].to_sym }
    if generic_field and generic_field[:options].has_key? :sort_sql
      universal_find_params[:order] = generic_field[:options][:sort_sql].gsub("%{DIRECTION}", sort_direction).gsub("%{OPPOSITE_DIRECTION}", sort_opposite_direction) 
      
    elsif self.columns_hash.has_key? options[:sort_by].to_s
      universal_find_params[:order] = "#{options[:sort_by]} #{sort_direction}"
    end
  end
        
  
  if respond_to? :limit_for_user
    if should_limit_index_for_created
      if defined? WillPaginate
        logger.info "#{self}.limit_with_security_scheme: limit_for_user & limit_index_for_created & will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
        limit_for_user(options[:user]).find(:all, universal_find_params).find(:all, :conditions => { creator_column => options[:user].id }).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
      else
        logger.info "#{self}.limit_with_security_scheme: limit_for_user & limit_index_for_created"
        limit_for_user(options[:user]).find(:all, universal_find_params).find(:all, :conditions => { creator_column => options[:user].id })
      end
    else
      if defined? WillPaginate
        logger.info "#{self}.limit_with_security_scheme: limit_for_user & will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
        limit_for_user(options[:user]).find(:all, universal_find_params).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
      else
        logger.info "#{self}.limit_with_security_scheme: limit_for_user"
        limit_for_user(options[:user]).find(:all, universal_find_params)
      end
    end
  else
    if should_limit_index_for_created
      if defined? WillPaginate
        logger.info "#{self}.limit_with_security_scheme: limit_index_for_created & will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
        find(:all, :conditions => { creator_column => options[:user].id }).find(:all, universal_find_params).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
      else
        logger.info "#{self}.limit_with_security_scheme: limit_index_for_created"
        find(:all, :conditions => { creator_column => options[:user].id }).find(:all, universal_find_params)
      end
    else
      if defined? WillPaginate
        logger.info "#{self}.limit_with_security_scheme: will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
        find(:all, universal_find_params).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
      else
        logger.info "#{self}.limit_with_security_scheme: (all)"
        find(:all, universal_find_params)
      end
    end
  end          
end

#named_scope(name, options = {}, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/qm-acts-as-generic-controller-model.rb', line 137

def named_scope(name, options = {}, &block)
  @named_scopes ||= {}
  @named_scopes[name.to_sym] = options.dup
  
  options.delete(:generic) if options.is_a? Hash
   
  options[:block] = block if block
  
  super name, options, &block
end

#named_scopesObject



123
124
125
# File 'lib/qm-acts-as-generic-controller-model.rb', line 123

def named_scopes
  @named_scopes || {}
end