module Libis::Workflow::ActiveRecord::Base::ClassMethods

Public Instance Methods

create_from_hash(hash, id_tags, &block) click to toggle source
# File lib/libis/workflow/activerecord/base.rb, line 27
def create_from_hash(hash, id_tags, &block)
  hash = hash.key_symbols_to_strings(recursive: true)
  id_tags = id_tags.map(&:to_s)
  return nil unless id_tags.empty? || id_tags.any? { |k| hash.include?(k) }
  tags = id_tags.inject({}) do |h, k|
    v = hash.delete(k)
    h[k] = v if v
    h
  end
  item = tags.empty? ? self.new : self.find_or_initialize_by(tags)
  block.call(item, hash) if block unless hash.empty?
  item.assign_attributes(hash)
  item.save!
  item
end
from_hash(hash) click to toggle source
# File lib/libis/workflow/activerecord/base.rb, line 23
def from_hash(hash)
  self.create_from_hash(hash.cleanup, [:name])
end