Module: PopulateMe::DocumentMixins::Schema::ClassMethods

Defined in:
lib/populate_me/document_mixins/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject



14
# File 'lib/populate_me/document_mixins/schema.rb', line 14

def fields; @fields ||= {}; end

#label_fieldObject

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

#batch_fieldObject



132
133
134
# File 'lib/populate_me/document_mixins/schema.rb', line 132

def batch_field
  @batch_field
end

#batch_on_field(sym) ⇒ Object

sets the batch_field



129
130
131
# File 'lib/populate_me/document_mixins/schema.rb', line 129

def batch_on_field sym # sets the batch_field
  @batch_field = sym.to_sym
end

#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
53
54
55
# File 'lib/populate_me/document_mixins/schema.rb', line 30

def complete_field_options 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)
  unless [:id, :polymorphic_type].include?(o[:type])
    complete_only_for_field_option o
  end
  if o[:type]==:attachment
    WebUtils.ensure_key! o, :class_name, settings.default_attachment_class
    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.guess_related_class_name(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

#complete_only_for_field_option(o = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/populate_me/document_mixins/schema.rb', line 57

def complete_only_for_field_option o={}
  if @currently_only_for
    o[:only_for] = @currently_only_for
  end
  if o[:only_for].is_a?(String)
    o[:only_for] = [o[:only_for]]
  end
  if o.key?(:only_for)
    self.polymorphic unless self.polymorphic?
    self.fields[:polymorphic_type][:values] += o[:only_for]
    self.fields[:polymorphic_type][:values].uniq!
  end
end

#default_select_fieldsObject



168
169
170
171
172
173
174
# File 'lib/populate_me/document_mixins/schema.rb', line 168

def default_select_fields
  [
    self.id_string_key,
    self.label_field,
    self.admin_image_field,
  ].compact.uniq
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? and o[:type]!=:id
  complete_field_options 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

#field_applicable?(f, p_type = nil) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/populate_me/document_mixins/schema.rb', line 101

def field_applicable? f, p_type=nil
  applicable? :fields, f, p_type
end

#label(sym) ⇒ Object

sets the label_field



118
119
120
# File 'lib/populate_me/document_mixins/schema.rb', line 118

def label sym # sets the label_field
  @label_field = sym.to_sym
end

#only_for(polymorphic_type_values, &bloc) ⇒ Object



91
92
93
94
95
# File 'lib/populate_me/document_mixins/schema.rb', line 91

def only_for polymorphic_type_values, &bloc
  @currently_only_for = polymorphic_type_values
  yield if block_given?
  remove_instance_variable(:@currently_only_for)
end

#polymorphic(o = {}) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/populate_me/document_mixins/schema.rb', line 82

def polymorphic o={}
  WebUtils.ensure_key! o, :type, :polymorphic_type
  WebUtils.ensure_key! o, :values, []
  WebUtils.ensure_key! o, :wrap, false
  WebUtils.ensure_key! o, :input_attributes, {}
  WebUtils.ensure_key! o[:input_attributes], :type, :hidden
  field(:polymorphic_type, o) unless self.polymorphic?
end

#polymorphic?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/populate_me/document_mixins/schema.rb', line 97

def polymorphic?
  self.fields.key? :polymorphic_type
end

#position_field(o = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/populate_me/document_mixins/schema.rb', line 75

def position_field o={}
  name = o[:name]||:position
  o[:type] = :position
  field name, o
  sort_by name, (o[:direction]||:asc)
end

#relationship(name, o = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/populate_me/document_mixins/schema.rb', line 148

def relationship name, o={}
  o[:class_name] = WebUtils.guess_related_class_name(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
  complete_only_for_field_option o
  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

#relationship_applicable?(f, p_type = nil) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/populate_me/document_mixins/schema.rb', line 105

def relationship_applicable? f, p_type=nil
  applicable? :relationships, f, p_type
end

#relationshipsObject



146
# File 'lib/populate_me/document_mixins/schema.rb', line 146

def relationships; @relationships ||= {}; end

#set_id_fieldObject



71
72
73
# File 'lib/populate_me/document_mixins/schema.rb', line 71

def set_id_field
  field :id, {type: :id}
end

#sort_by(f, direction = :asc) ⇒ Object

Raises:

  • (ArgumentError)


136
137
138
139
140
141
142
143
144
# File 'lib/populate_me/document_mixins/schema.rb', line 136

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/populate_me/document_mixins/schema.rb', line 176

def to_select_options o={}
  proc do
    items = self.admin_find({
      query: ( o[:query] || {} ),
      fields: ( o[:fields] || default_select_fields ),
    })
    output = items.sort_by do |i|
      i.to_s.downcase
    end.map do |i|
      item = { description: i.to_s, value: i.id }
      unless self.admin_image_field.nil?
        item.merge! preview_uri: i.admin_image_url
      end
      item
    end
    output.unshift(description: '?', value: '') if o[:allow_empty]
    output
  end
end