Module: PopulateMe::DocumentMixins::Schema::ClassMethods
- Defined in:
- lib/populate_me/document_mixins/schema.rb
Instance Attribute Summary collapse
- #fields ⇒ Object
-
#label_field ⇒ Object
Returns the value of attribute label_field.
Instance Method Summary collapse
- #complete_field_options(name, o = {}) ⇒ Object
- #field(name, o = {}) ⇒ Object
-
#label(sym) ⇒ Object
sets the label_field.
- #position_field(o = {}) ⇒ Object
- #relationship(name, o = {}) ⇒ Object
- #relationships ⇒ Object
- #set_id_field ⇒ Object
- #sort_by(f, direction = :asc) ⇒ Object
- #to_select_options(o = {}) ⇒ Object
Instance Attribute Details
#fields ⇒ Object
14 |
# File 'lib/populate_me/document_mixins/schema.rb', line 14 def fields; @fields ||= {}; end |
#label_field ⇒ Object
Returns the value of attribute label_field.
12 13 14 |
# File 'lib/populate_me/document_mixins/schema.rb', line 12 def label_field @label_field end |
Instance Method Details
#complete_field_options(name, o = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/populate_me/document_mixins/schema.rb', line 30 def name, o={} o[:field_name] = name WebUtils.ensure_key! o, :type, :string WebUtils.ensure_key! o, :form_field, ![:id,:position].include?(o[:type]) o[:wrap] = false unless o[:form_field] WebUtils.ensure_key! o, :wrap, ![:hidden,:list].include?(o[:type]) WebUtils.ensure_key! o, :label, WebUtils.label_for_field(name) if o[:type]==:attachment WebUtils.ensure_key! o, :class_name, settings. raise MissingAttachmentClassError, "No attachment class was provided for the #{self.name} field: #{name}" if o[:class_name].nil? o[:class_name] = o[:class_name].name unless o[:class_name].is_a?(String) end if o[:type]==:list o[:class_name] = WebUtils.(self.name,o[:class_name]||name) o[:dasherized_class_name] = WebUtils.dasherize_class_name o[:class_name] else WebUtils.ensure_key! o, :input_attributes, {} o[:input_attributes][:type] = :hidden if o[:type]==:hidden unless o[:type]==:text WebUtils.ensure_key! o[:input_attributes], :type, :text end end end |
#field(name, o = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/populate_me/document_mixins/schema.rb', line 16 def field name, o={} set_id_field if self.fields.empty?&&o[:type]!=:id name, o if o[:type]==:list define_method(name) do var = "@#{name}" instance_variable_set(var, instance_variable_get(var)||[]) end else attr_accessor name end self.fields[name] = o end |
#label(sym) ⇒ Object
sets the label_field
65 66 67 |
# File 'lib/populate_me/document_mixins/schema.rb', line 65 def label sym # sets the label_field @label_field = sym.to_sym end |
#position_field(o = {}) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/populate_me/document_mixins/schema.rb', line 58 def position_field o={} name = o[:name]||:position o[:type] = :position field name, o sort_by name end |
#relationship(name, o = {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/populate_me/document_mixins/schema.rb', line 85 def relationship name, o={} o[:class_name] = WebUtils.(self.name,o[:class_name]||name) WebUtils.ensure_key! o, :label, name.to_s.gsub('_',' ').capitalize WebUtils.ensure_key! o, :foreign_key, "#{WebUtils.dasherize_class_name(self.name).gsub('-','_')}_id" o[:foreign_key] = o[:foreign_key].to_sym WebUtils.ensure_key! o, :dependent, true self.relationships[name] = o define_method(name) do var = "@cached_#{name}" instance_variable_set(var, instance_variable_get(var)||WebUtils.resolve_class_name(o[:class_name]).admin_find(query: {o[:foreign_key]=>self.id})) end define_method("#{name}_first".to_sym) do var = "@cached_#{name}_first" instance_variable_set(var, instance_variable_get(var)||WebUtils.resolve_class_name(o[:class_name]).admin_find_first(query: {o[:foreign_key]=>self.id})) end end |
#relationships ⇒ Object
83 |
# File 'lib/populate_me/document_mixins/schema.rb', line 83 def relationships; @relationships ||= {}; end |
#set_id_field ⇒ Object
54 55 56 |
# File 'lib/populate_me/document_mixins/schema.rb', line 54 def set_id_field field :id, {type: :id} end |
#sort_by(f, direction = :asc) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/populate_me/document_mixins/schema.rb', line 73 def sort_by f, direction=:asc raise(ArgumentError) unless [:asc,:desc].include? direction raise(ArgumentError) unless self.new.respond_to? f @sort_proc = Proc.new do |a,b| a,b = b,a if direction==:desc a.__send__(f)<=>b.__send__(f) end self end |
#to_select_options(o = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/populate_me/document_mixins/schema.rb', line 104 def o={} proc do items = self.admin_find(query: {}, fields: [self.id_string_key, self.label_field]) output = items.sort_by do |i| i.to_s.downcase end.map do |i| [i.to_s, i.id] end output.unshift(['?','']) if o[:allow_empty] output end end |