Sha256: 343d83ab5bbc7ba911a4ce4c3945e2c9dbf9cc995bc6e5313eb335ff74986271

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

module RailsWorkflow
  class Context < ActiveRecord::Base
    belongs_to :parent, polymorphic: true
    attr_accessor :data

    serialize :body, JSON

    before_save :serialize_data
    after_find :init_data

    def serialize_data
      self.body = prepare_data(self.data)
    end

    def init_data
      self.data = prepare_body(body).with_indifferent_access
    end

    def prepare_body body
      if body.is_a? Array
        body.map do |element|
          prepare_body element
        end
      elsif body.is_a? Hash

        if body.keys == ["id", "class"]
          body["class"].constantize.find(body["id"])
        else
          res = {}
          body.each_pair do |key, value|
            res[key] = prepare_body(value)
          end
          res
        end

      else
        body
      end
    end


    def prepare_data data
      if data.is_a? ActiveRecord::Base
        { id: data.id, class: data.class.to_s }
      elsif data.is_a? Array
        data.map do |element|
          prepare_data element
        end
      elsif data.is_a? Hash
        res = {}
        data.each_pair do |key, value|
          res[key] = prepare_data(value)
        end
        res
      else
        data
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rails_workflow-0.3.7 app/models/rails_workflow/context.rb
rails_workflow-0.3.6 app/models/rails_workflow/context.rb
rails_workflow-0.3.5 app/models/rails_workflow/context.rb
rails_workflow-0.3.4 app/models/rails_workflow/context.rb
rails_workflow-0.3.3 app/models/rails_workflow/context.rb
rails_workflow-0.3.2 app/models/rails_workflow/context.rb
rails_workflow-0.3.1 app/models/rails_workflow/context.rb