Sha256: 9c5c8b6dffe63824a1bcb6c0dd90463112ab10ff07a24b8578ba46a22498d9cb

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Contextify
  module UserHelper
    def self.included(base)
      base.extend(ClassMethods)
    end

    # TODO: Catch errors because of missing methods and prompt the
    # user, "Tried to sync a non-existant method with
    # contextify. Please make sure to implement #{method_name} on your
    # model"
    def contextify_sync!
      Contextify.post(self.email, contextify_data)
    end

    def contextify_data(*fields)
      fields = fields.empty? ? self.class.class_variable_get('@@contextify_fields') : fields

      fields.inject({}) { |run, field| run[field] = self.send(field.to_sym); run }
    end

    module ClassMethods
      def self.extended(base)
        base.class_variable_set("@@contextify_fields", [])

        Contextify.model = base.to_s
      end

      def contextify_fields=(*args)
        self.class_variable_set("@@contextify_fields", args)
      end

      def contextify_user(*fields)
        self.send(:contextify_fields=, *fields)

        after_commit :contextify_sync!
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zenbox-0.0.3 lib/contextify/user_helper.rb
zenbox-0.0.2 lib/contextify/user_helper.rb