Sha256: 5293af5dc399a21416522262c3f9052d723778f675d81a0e270df5bf211a86aa

Contents?: true

Size: 981 Bytes

Versions: 1

Compression:

Stored size: 981 Bytes

Contents

module Zenbox
  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
    # zenbox. Please make sure to implement #{method_name} on your
    # model"
    def zenbox_sync!
      Zenbox.post(self.email, zenbox_data)
    end

    def zenbox_data(*fields)
      fields = fields.empty? ? self.class.class_variable_get('@@zenbox_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("@@zenbox_fields", [])

        Zenbox.model = base.to_s
      end

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

      def zenbox_user(*fields)
        self.send(:zenbox_fields=, *fields)

        after_commit :zenbox_sync!
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zenbox-0.0.4 lib/zenbox/user_helper.rb