Sha256: a85612fb0904a0809818f51e0bb529dd9d6476b31e5f80147002b39e36b9bba5
Contents?: true
Size: 1.74 KB
Versions: 2
Compression:
Stored size: 1.74 KB
Contents
require 'active_support/inflector' module Swiftype class BaseModel < OpenStruct include Swiftype::Connection include Swiftype::Request class << self attr_reader :parent_classes def model_name name.split('::').last.underscore end def collection_name model_name.pluralize end def parents(*parent_classes) @parent_classes = parent_classes end end def to_hash table end def to_json to_hash.to_json end def create! update_with! post(path_to_collection, {self.class.model_name => to_hash}) end def update! update_with! put(path_to_model, {self.class.model_name => to_hash}) end def destroy! !!delete(path_to_model) end def path_to_model "#{raw_path_to_model}.json" end def raw_path_to_model path = (self.class.parent_classes || []).inject("") do |_, parent| parent_id = send("#{parent.model_name}_id") _ += "#{parent.collection_name}/#{parent_id}/" _ end "#{path}#{self.class.collection_name}/#{identifier}" end def path_to_collection "#{raw_path_to_collection}.json" end def raw_path_to_collection path = (self.class.parent_classes || []).inject("") do |_, parent| parent_id = send("#{parent.model_name}_id") _ += "#{parent.collection_name}/#{parent_id}/" _ end "#{path}#{self.class.collection_name}" end def update_with!(hash) hash.each do |k, v| send "#{k}=", v end self end def reload update_with! get(path_to_model) end def identifier slugged? ? slug : id end def slugged? respond_to?(:slug) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
swiftype-0.0.5 | lib/swiftype/base_model.rb |
swiftype-0.0.4 | lib/swiftype/base_model.rb |