Class: AzaharaSchema::Schema
- Inherits:
-
Object
- Object
- AzaharaSchema::Schema
- Defined in:
- lib/azahara_schema/schema.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#association ⇒ Object
Returns the value of attribute association.
-
#default_scope ⇒ Object
Returns the value of attribute default_scope.
-
#enabled_outputs ⇒ Object
Returns the value of attribute enabled_outputs.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#model ⇒ Object
Returns the value of attribute model.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#parent_schema ⇒ Object
Returns the value of attribute parent_schema.
-
#search_query ⇒ Object
Returns the value of attribute search_query.
Class Method Summary collapse
- .attribute(model, name, attr_type = nil) ⇒ Object
- .attribute_for_column(model, col, attr_type = nil) ⇒ Object
- .attribute_type(model, name, col = nil) ⇒ Object
- .enabled_filters(*filter_names) ⇒ Object
- .filter_operators(filter, operators) ⇒ Object
- .operators_for_filters ⇒ Object
- .schema_class_for(klass) ⇒ Object
- .schema_for(klass, **attributes) ⇒ Object
Instance Method Summary collapse
- #add_filter(name, operator, values) ⇒ Object
-
#add_short_filter(name, str) ⇒ Object
ACCESSORS.
- #add_sort(name, order = :asc) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #association_path ⇒ Object
- #attribute(name) ⇒ Object
- #attribute_for_column(col) ⇒ Object
- #available_associations ⇒ Object
- #available_attributes ⇒ Object
- #available_attributes_hash ⇒ Object
- #available_columns ⇒ Object
- #available_filters ⇒ Object
- #base_scope ⇒ Object
- #build_json_options!(options = {}) ⇒ Object
- #collapsable_filters ⇒ Object
- #column_names ⇒ Object
- #column_names=(values) ⇒ Object
- #columns ⇒ Object
- #default_columns ⇒ Object
-
#default_outputs ⇒ Object
DEFAULTS.
- #default_sort ⇒ Object
- #disabled_filters ⇒ Object
- #enabled_filter_names ⇒ Object
- #enabled_filters ⇒ Object
- #entities ⇒ Object
- #entity_as_json(entity, options = nil) ⇒ Object
- #entity_count ⇒ Object
- #entity_scope(scope = base_scope) ⇒ Object
- #filtered_scope ⇒ Object
- #filters ⇒ Object
-
#from_params(params) ⇒ Object
serialization.
- #include_associated_attributes? ⇒ Boolean
-
#initialize(model, **attributes) ⇒ Schema
constructor
A new instance of Schema.
- #initialize_available_attributes ⇒ Object
- #inspect ⇒ Object
-
#main_attribute_name ⇒ Object
just a dummy implementation.
- #nested_relations_depth ⇒ Object
- #operator_for(fname) ⇒ Object
- #operators_for(filter_name) ⇒ Object
- #outputs ⇒ Object
- #searchable_attributes ⇒ Object
- #sort ⇒ Object
- #to_param ⇒ Object
- #tokenize_search_query(query = search_query) ⇒ Object
- #uncollapsable_filters ⇒ Object
- #user_available_filters ⇒ Object
- #value_for(fname) ⇒ Object
Constructor Details
#initialize(model, **attributes) ⇒ Schema
Returns a new instance of Schema.
70 71 72 73 74 75 76 77 |
# File 'lib/azahara_schema/schema.rb', line 70 def initialize(model, **attributes) @model = model @association = attributes[:association] @parent_schema = attributes[:parent_schema] @column_names = attributes[:columns] @enabled_outputs = attributes[:outputs] || default_outputs @nested_relations_depth = attributes[:relations_depth] end |
Instance Attribute Details
#association ⇒ Object
Returns the value of attribute association.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def association @association end |
#default_scope ⇒ Object
Returns the value of attribute default_scope.
67 68 69 |
# File 'lib/azahara_schema/schema.rb', line 67 def default_scope @default_scope end |
#enabled_outputs ⇒ Object
Returns the value of attribute enabled_outputs.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def enabled_outputs @enabled_outputs end |
#limit ⇒ Object
Returns the value of attribute limit.
68 69 70 |
# File 'lib/azahara_schema/schema.rb', line 68 def limit @limit end |
#model ⇒ Object
Returns the value of attribute model.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def model @model end |
#offset ⇒ Object
Returns the value of attribute offset.
68 69 70 |
# File 'lib/azahara_schema/schema.rb', line 68 def offset @offset end |
#parent_schema ⇒ Object
Returns the value of attribute parent_schema.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def parent_schema @parent_schema end |
#search_query ⇒ Object
Returns the value of attribute search_query.
67 68 69 |
# File 'lib/azahara_schema/schema.rb', line 67 def search_query @search_query end |
Class Method Details
.attribute(model, name, attr_type = nil) ⇒ Object
44 45 46 47 |
# File 'lib/azahara_schema/schema.rb', line 44 def self.attribute(model, name, attr_type=nil) attr_type ||= attribute_type(model, name) Attribute.new(model, name, attr_type) end |
.attribute_for_column(model, col, attr_type = nil) ⇒ Object
49 50 51 |
# File 'lib/azahara_schema/schema.rb', line 49 def self.attribute_for_column(model, col, attr_type=nil) attribute(model, col.name, attr_type || attribute_type(model, col.name, col) ) end |
.attribute_type(model, name, col = nil) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/azahara_schema/schema.rb', line 35 def self.attribute_type(model, name, col=nil) col ||= model.columns_hash[name.to_s] if model.defined_enums[col.name] 'list' else col && col.type end end |
.enabled_filters(*filter_names) ⇒ Object
53 54 55 56 |
# File 'lib/azahara_schema/schema.rb', line 53 def self.enabled_filters(*filter_names) @enabled_filters = filter_names if filter_names.any? @enabled_filters ||= [] end |
.filter_operators(filter, operators) ⇒ Object
62 63 64 |
# File 'lib/azahara_schema/schema.rb', line 62 def self.filter_operators(filter, operators) operators_for_filters[filter] = operators end |
.operators_for_filters ⇒ Object
58 59 60 |
# File 'lib/azahara_schema/schema.rb', line 58 def self.operators_for_filters @operators_for_filters ||= {} end |
.schema_class_for(klass) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/azahara_schema/schema.rb', line 6 def self.schema_class_for(klass) klasses = [klass] while klass != klass.base_class klass = klass.superclass klasses << klass end klasses.each do |kls| schema_klass = "#{kls.name}Schema".safe_constantize return schema_klass if schema_klass end AzaharaSchema::Schema end |
.schema_for(klass, **attributes) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/azahara_schema/schema.rb', line 19 def self.schema_for(klass, **attributes) model = klass klasses = [klass] while klass != klass.base_class klass = klass.superclass klasses << klass end klasses.each do |kls| schema_klass = "#{kls.name}Schema".safe_constantize if schema_klass return kls.try(:abstract_class?) ? schema_klass.new(model, **attributes) : schema_klass.new(**attributes) end end AzaharaSchema::Schema.new(model, **attributes) end |
Instance Method Details
#add_filter(name, operator, values) ⇒ Object
147 148 149 150 |
# File 'lib/azahara_schema/schema.rb', line 147 def add_filter(name, operator, values) raise 'filter ('+name+') is not defined!' unless available_filters.key?(name) filters[name] = { o: operator, v: values } end |
#add_short_filter(name, str) ⇒ Object
ACCESSORS
137 138 139 140 141 142 143 144 145 |
# File 'lib/azahara_schema/schema.rb', line 137 def add_short_filter(name, str) attrs = str.split('|') if attrs.size == 2 operator, values = attrs elsif attrs.size == 1 operator, values = '=', attrs.first end add_filter(name, operator, values.split('\\')) end |
#add_sort(name, order = :asc) ⇒ Object
152 153 154 |
# File 'lib/azahara_schema/schema.rb', line 152 def add_sort(name, order=:asc) sort[name] = order end |
#as_json(options = {}) ⇒ Object
322 323 324 325 |
# File 'lib/azahara_schema/schema.rb', line 322 def as_json(={}) () entities.collect{|entity| entity_as_json(entity, ) } end |
#association_path ⇒ Object
213 214 215 |
# File 'lib/azahara_schema/schema.rb', line 213 def association_path @association_path ||= parent_schema ? ( parent_schema.association_path + [association.name] ) : [model.model_name.element.to_sym] end |
#attribute(name) ⇒ Object
170 171 172 |
# File 'lib/azahara_schema/schema.rb', line 170 def attribute(name) available_attributes.detect{|att| att.name == name} end |
#attribute_for_column(col) ⇒ Object
229 230 231 |
# File 'lib/azahara_schema/schema.rb', line 229 def attribute_for_column(col) self.class.attribute_for_column(model, col) end |
#available_associations ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/azahara_schema/schema.rb', line 217 def available_associations return [] unless include_associated_attributes? @available_associations ||= model.reflect_on_all_associations.select do |association| !association.[:polymorphic] && association.klass != model && !association_path.include?( association.name.to_s.singularize.to_sym ) && !association_path.include?( association.name.to_s.pluralize.to_sym ) end.collect do |association| AzaharaSchema::Schema.schema_for(association.klass, parent_schema: self, association: association, relations_depth: nested_relations_depth.try(:-, 1)) end end |
#available_attributes ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/azahara_schema/schema.rb', line 174 def available_attributes unless @available_attributes initialize_available_attributes @available_attributes.each{|at| at.table_alias = Array(association_path[1..-1]).collect(&:to_s).join('_').presence } end @available_attributes end |
#available_attributes_hash ⇒ Object
182 183 184 |
# File 'lib/azahara_schema/schema.rb', line 182 def available_attributes_hash available_attributes.inject({}){|obj, aa| obj[aa.name] = aa; obj } end |
#available_columns ⇒ Object
186 187 188 |
# File 'lib/azahara_schema/schema.rb', line 186 def available_columns @available_columns ||= available_attributes.select{|att| att.column? } end |
#available_filters ⇒ Object
205 206 207 |
# File 'lib/azahara_schema/schema.rb', line 205 def available_filters @available_filters ||= available_attributes_hash end |
#base_scope ⇒ Object
256 257 258 |
# File 'lib/azahara_schema/schema.rb', line 256 def base_scope model.all end |
#build_json_options!(options = {}) ⇒ Object
298 299 300 301 |
# File 'lib/azahara_schema/schema.rb', line 298 def (={}) columns.each{|col| col.() } end |
#collapsable_filters ⇒ Object
378 379 380 |
# File 'lib/azahara_schema/schema.rb', line 378 def collapsable_filters available_filters end |
#column_names ⇒ Object
92 93 94 |
# File 'lib/azahara_schema/schema.rb', line 92 def column_names @column_names ||= default_columns end |
#column_names=(values) ⇒ Object
87 88 89 90 |
# File 'lib/azahara_schema/schema.rb', line 87 def column_names=(values) @column_names = values @columns = nil end |
#columns ⇒ Object
96 97 98 |
# File 'lib/azahara_schema/schema.rb', line 96 def columns @columns ||= available_attributes_hash.slice(*column_names).values end |
#default_columns ⇒ Object
114 115 116 |
# File 'lib/azahara_schema/schema.rb', line 114 def default_columns [main_attribute_name] end |
#default_outputs ⇒ Object
DEFAULTS
110 111 112 |
# File 'lib/azahara_schema/schema.rb', line 110 def default_outputs [AzaharaSchema::Outputs.registered_outputs.keys.first].compact end |
#default_sort ⇒ Object
118 119 120 |
# File 'lib/azahara_schema/schema.rb', line 118 def default_sort {} end |
#disabled_filters ⇒ Object
194 195 196 |
# File 'lib/azahara_schema/schema.rb', line 194 def disabled_filters [] end |
#enabled_filter_names ⇒ Object
198 199 200 201 202 203 |
# File 'lib/azahara_schema/schema.rb', line 198 def enabled_filter_names names = self.class.enabled_filters if self.class.enabled_filters.any? names ||= available_attributes_hash.keys names &= enabled_filters if enabled_filters names -= disabled_filters end |
#enabled_filters ⇒ Object
190 191 192 |
# File 'lib/azahara_schema/schema.rb', line 190 def enabled_filters end |
#entities ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/azahara_schema/schema.rb', line 284 def entities scope = filtered_scope columns.each do |col| scope = col.add_preload(scope) end sort.keys.reverse.each do |name| att = attribute(name) scope = att.add_sort(scope, sort[name]) if att end scope = scope.offset(offset) if offset scope = scope.limit(limit) if limit scope end |
#entity_as_json(entity, options = nil) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/azahara_schema/schema.rb', line 303 def entity_as_json(entity, =nil) attr_hash = entity.as_json() # TODO serializable_add_includes(options) do |association, records, opts| columns.each do |col| col_sub_hash = attr_hash sub_col = col while sub_col.is_a?(AzaharaSchema::AssociationAttribute) col_sub_hash = (col_sub_hash[sub_col.association.name.to_s] ||= {}) sub_col = sub_col.attribute end if col.type == 'love' col_sub_hash[sub_col.name] = sub_col.available_values.detect{|l, v| v == col_sub_hash[sub_col.name] }.try(:[], 0) else col_sub_hash[sub_col.name] = col.value(entity) end end attr_hash end |
#entity_count ⇒ Object
280 281 282 |
# File 'lib/azahara_schema/schema.rb', line 280 def entity_count filtered_scope.count end |
#entity_scope(scope = base_scope) ⇒ Object
260 261 262 263 264 |
# File 'lib/azahara_schema/schema.rb', line 260 def entity_scope(scope=base_scope) scope = scope.visible if scope.respond_to?(:visible) scope = scope.send(self.default_scope) if self.default_scope scope end |
#filtered_scope ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/azahara_schema/schema.rb', line 266 def filtered_scope scope = entity_scope filters.each do |name, attrs| scope = available_filters[name].add_statement(scope, attrs[:o], attrs[:v]) end if (tokens = tokenize_search_query) searchable_attributes.each{|a| scope = a.add_join(scope) } arl = searchable_attributes[0].arel_statement('~', tokens) if searchable_attributes.any? Array(searchable_attributes[1..-1]).each{|att| arl = arl.or( att.arel_statement('~', tokens) ) } scope = scope.where(arl) end scope end |
#filters ⇒ Object
100 101 102 |
# File 'lib/azahara_schema/schema.rb', line 100 def filters @filters ||= {} end |
#from_params(params) ⇒ Object
serialization
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/azahara_schema/schema.rb', line 329 def from_params(params) if params[:f] if params.class == ActionController::Parameters filter_params = params[:f].permit(available_filters.keys + [available_filters.keys.inject({}){|o,name| o[name] = []; o }]).to_h else filter_params = params[:f].slice(available_filters.keys + [available_filters.keys.inject({}){|o,name| o[name] = []; o }]).to_h end filter_params.each do |name, filter_value| next if filter_value.blank? if filter_value.is_a?(Array) add_filter(name, '=', filter_value) else add_short_filter(name, filter_value) end end end if params[:c].is_a?(Array) self.column_names = params[:c].to_a end if params[:sort] @sort = nil params[:sort].each do |k, sort| add_sort(sort[:path], sort['desc'] == 'true' ? :desc : :asc ) end end self.default_scope = params[:default_scope] if params[:default_scope] self.search_query = params[:q] unless params[:q].blank? self.offset = params[:offset] if params[:offset] self.limit = params[:limit] if params[:limit] end |
#include_associated_attributes? ⇒ Boolean
131 132 133 |
# File 'lib/azahara_schema/schema.rb', line 131 def include_associated_attributes? nested_relations_depth > 0 end |
#initialize_available_attributes ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/azahara_schema/schema.rb', line 233 def initialize_available_attributes @available_attributes ||= [] model.columns.each do |col| @available_attributes << attribute_for_column(col) end available_associations.each do |asoc_schema| asoc_schema.available_attributes.each do |asoc_attribute| next if asoc_attribute.is_a?(AggregationAttribute) added_attribute = AssociationAttribute.new(model, asoc_schema, asoc_attribute) @available_attributes << added_attribute @available_attributes << AggregationAttribute.new(model, added_attribute) if asoc_attribute.aggregable? end end end |
#inspect ⇒ Object
79 80 81 |
# File 'lib/azahara_schema/schema.rb', line 79 def inspect to_s end |
#main_attribute_name ⇒ Object
just a dummy implementation
123 124 125 |
# File 'lib/azahara_schema/schema.rb', line 123 def main_attribute_name available_attributes.detect{|att| att.name != 'id' }.name end |
#nested_relations_depth ⇒ Object
127 128 129 |
# File 'lib/azahara_schema/schema.rb', line 127 def nested_relations_depth @nested_relations_depth || 4 end |
#operator_for(fname) ⇒ Object
156 157 158 |
# File 'lib/azahara_schema/schema.rb', line 156 def operator_for(fname) filters[fname] && filters[fname][:o] end |
#operators_for(filter_name) ⇒ Object
164 165 166 167 168 |
# File 'lib/azahara_schema/schema.rb', line 164 def operators_for(filter_name) operators = available_filters[filter_name] && available_filters[filter_name].available_operators operators &= self.class.operators_for_filters[filter_name] if operators && self.class.operators_for_filters[filter_name] operators end |
#outputs ⇒ Object
248 249 250 |
# File 'lib/azahara_schema/schema.rb', line 248 def outputs Outputs.new(self) end |
#searchable_attributes ⇒ Object
83 84 85 |
# File 'lib/azahara_schema/schema.rb', line 83 def searchable_attributes @searchable_attributes ||= available_attributes.select{|a| a.searchable? } end |
#sort ⇒ Object
104 105 106 |
# File 'lib/azahara_schema/schema.rb', line 104 def sort @sort ||= ActiveSupport::OrderedHash.new.merge!(default_sort) end |
#to_param ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/azahara_schema/schema.rb', line 360 def to_param params = {} params[:f] = {} filters.each do |fname, attrs| params[:f][fname] = "#{attrs[:o]}|#{Array(attrs[:v]).collect{|v| v.to_s}.join('\\')}" end params[:default_scope] = default_scope if default_scope params[:c] = column_names params[:q] = search_query if params[:q] params[:offset] = offset params[:limit] = limit params end |
#tokenize_search_query(query = search_query) ⇒ Object
252 253 254 |
# File 'lib/azahara_schema/schema.rb', line 252 def tokenize_search_query(query=search_query) query.split if query end |
#uncollapsable_filters ⇒ Object
374 375 376 |
# File 'lib/azahara_schema/schema.rb', line 374 def uncollapsable_filters {} end |
#user_available_filters ⇒ Object
209 210 211 |
# File 'lib/azahara_schema/schema.rb', line 209 def user_available_filters available_filters.slice(*enabled_filter_names) end |
#value_for(fname) ⇒ Object
160 161 162 |
# File 'lib/azahara_schema/schema.rb', line 160 def value_for(fname) filters[fname] && filters[fname][:v] end |