Skip to content

Instantly share code, notes, and snippets.

@Talleyran
Created October 30, 2011 10:57
Show Gist options
  • Save Talleyran/1325782 to your computer and use it in GitHub Desktop.
Save Talleyran/1325782 to your computer and use it in GitHub Desktop.
модель Layer
class Layer
include Mongoid::Document
include Mongoid::Timestamps
#TODO create geotype filter
field :geotypes, type: Array
field :structure_json, type: String
field :is_spatial, type: Boolean, default: true
field :has_many_geoms, type: Boolean, default: false
embeds_many :layers_objects
before_validation :set_fields
after_save :clear_temp_attributes
attr_accessor :structure
def structure
if new_record?
@structure ||= attributes["structure"]
else
t = {}
JSON.parse(structure_json).each{|k,v| t[k.to_sym] = v.to_sym}
@structure ||= t
end
end
def new_layers_object attrs
return if structure.blank?
layers_object = self.layers_objects.new
structure.each{|k,t|
layers_object.fields_objects.new key: k, value: attrs[k], field_type: t
}
layers_object
end
private
def set_fields
self.structure_json = structure.to_json
end
def clear_temp_attributes
@structure = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment