Module: QM::ActsAsGenericController::ModelIncludes::ClassMethods
- Defined in:
- lib/qm-acts-as-generic-controller-model.rb
Instance Method Summary collapse
- #belongs_to(association_id, options = {}) ⇒ Object
- #generic_actions(options = {}) ⇒ Object
- #generic_field_associations ⇒ Object
- #generic_fields(options = {}) ⇒ Object
- #generic_form_fieldsets ⇒ Object
- #generic_named_scopes ⇒ Object
- #has_and_belongs_to_many(association_id, options = {}, &extension) ⇒ Object
- #has_generic_action_for(actions, name, options = {}) ⇒ Object
- #has_generic_field_for(actions, name, options = {}) ⇒ Object
- #has_generic_form_fieldsets(*fieldsets) ⇒ Object
- #has_generic_named_scopes? ⇒ Boolean
- #has_many(association_id, options = {}, &extension) ⇒ Object
- #limit_with_security_scheme(options) ⇒ Object
- #named_scope(name, options = {}, &block) ⇒ Object
- #named_scopes ⇒ Object
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, = {}) @generic_field_associations ||= {} unless .has_key?(:polymorphic) klass = ( .has_key?(:class_name) ? [: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 => [:foreign_key] || "#{klass.to_s.tableize}_id" } end super association_id, 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( = {}) @generic_actions ||= {} if .has_key? :action @generic_actions[[:action]] || [] else @generic_actions || [] end end |
#generic_field_associations ⇒ Object
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( = {}) @generic_fields ||= {} if .has_key? :action @generic_fields[[:action]] || [] else @generic_fields || [] end end |
#generic_form_fieldsets ⇒ Object
166 167 168 |
# File 'lib/qm-acts-as-generic-controller-model.rb', line 166 def generic_form_fieldsets @generic_form_fieldsets end |
#generic_named_scopes ⇒ Object
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, = {}, &extension) @generic_field_associations ||= {} @generic_field_associations[association_id.to_sym] = { :kind => :has_and_belongs_to_many, :class_name => ( .has_key?(:class_name) ? [:class_name].constantize : association_id.to_s.classify.constantize ), :readonly => [:readonly] || false, :through => [:through].present?, :generic_create => [:generic_create] || false } .delete :generic_create super association_id, , &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, = {}) = .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 => } 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 => } 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, = {}) = .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 => } 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 => } 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
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, = {}, &extension) @generic_field_associations ||= {} @generic_field_associations[association_id.to_sym] = { :kind => :has_many, :class_name => ( .has_key?(:class_name) ? [:class_name].constantize : association_id.to_s.classify.constantize ), :readonly => [:readonly] || false, :through => [:through].present?, :generic_create => [:generic_create] || false } .delete :generic_create super association_id, , &extension end |
#limit_with_security_scheme(options) ⇒ Object
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[: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 .has_key? :user return all if [:user].is_a? ExternalSystem # Return empty set if user has no privileges at all (nor index_any and index_created) unless [:user].has_privileges?(:class_name => self, :generic_action => :index_any) or [:user].has_privileges?(:class_name => self, :generic_action => :index_created) return [] end creator_column = "creator_#{[:user].class.to_s.tableize.singularize}_id" should_limit_index_for_created = columns_hash.has_key?(creator_column) && [:user].respond_to?(:has_privileges?) && ![:user].has_privileges?(:class_name => self, :generic_action => :index_any) && [:user].has_privileges?(:class_name => self, :generic_action => :index_created) universal_find_params = {} if .has_key? :sort_by and not [:sort_by].nil? and not [:sort_by].to_s.empty? # Sorting if [: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] == [: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? [:sort_by].to_s universal_find_params[:order] = "#{[: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 = #{[:paginate_page]}, per_page = #{[:paginate_per_page]})" limit_for_user([:user]).find(:all, universal_find_params).find(:all, :conditions => { creator_column => [:user].id }).paginate(:page => [:paginate_page], :per_page => [:paginate_per_page]) else logger.info "#{self}.limit_with_security_scheme: limit_for_user & limit_index_for_created" limit_for_user([:user]).find(:all, universal_find_params).find(:all, :conditions => { creator_column => [:user].id }) end else if defined? WillPaginate logger.info "#{self}.limit_with_security_scheme: limit_for_user & will_paginate(page = #{[:paginate_page]}, per_page = #{[:paginate_per_page]})" limit_for_user([:user]).find(:all, universal_find_params).paginate(:page => [:paginate_page], :per_page => [:paginate_per_page]) else logger.info "#{self}.limit_with_security_scheme: limit_for_user" limit_for_user([: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 = #{[:paginate_page]}, per_page = #{[:paginate_per_page]})" find(:all, :conditions => { creator_column => [:user].id }).find(:all, universal_find_params).paginate(:page => [:paginate_page], :per_page => [:paginate_per_page]) else logger.info "#{self}.limit_with_security_scheme: limit_index_for_created" find(:all, :conditions => { creator_column => [:user].id }).find(:all, universal_find_params) end else if defined? WillPaginate logger.info "#{self}.limit_with_security_scheme: will_paginate(page = #{[:paginate_page]}, per_page = #{[:paginate_per_page]})" find(:all, universal_find_params).paginate(:page => [:paginate_page], :per_page => [: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, = {}, &block) @named_scopes ||= {} @named_scopes[name.to_sym] = .dup .delete(:generic) if .is_a? Hash [:block] = block if block super name, , &block end |
#named_scopes ⇒ Object
123 124 125 |
# File 'lib/qm-acts-as-generic-controller-model.rb', line 123 def named_scopes @named_scopes || {} end |