Sha256: 840c7f606c4ed46e38844f1579ad0ecac95d118a65a395b03556276a16fba5ef

Contents?: true

Size: 982 Bytes

Versions: 4

Compression:

Stored size: 982 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_model(*fields)
        self.send(:zenbox_fields=, *fields)

        after_commit :zenbox_sync!
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zenbox-0.0.8 lib/zenbox/user_helper.rb
zenbox-0.0.7 lib/zenbox/user_helper.rb
zenbox-0.0.6 lib/zenbox/user_helper.rb
zenbox-0.0.5 lib/zenbox/user_helper.rb