module OrbitModel
  module Status

    def self.included(base)
      base.extend ClassMethods
      base.field :is_top, :type => Boolean, :default => false 
      base.field :is_hot, :type => Boolean, :default => false 
      base.field :is_hidden, :type => Boolean, :default => false

      def is_top?
        self.is_top
      end

      def is_hot?
        self.is_hot
      end

      def is_hidden?
        self.is_hidden
      end

      def statuses
        statuses = []
        statuses << I18n.t(:top) if is_top?
        statuses << I18n.t(:hot) if is_hot?
        statuses << I18n.t(:hidden) if is_hidden?
        statuses
      end

      def statuses_with_classname
        statuses = []
        statuses << {"name" => I18n.t(:top), "classname" => "top"} if is_top?
        statuses << {"name" => I18n.t(:hot), "classname" => "hot"} if is_hot?
        statuses << {"name" => I18n.t(:hidden), "classname" => "hidden"} if is_hidden?
        statuses
      end

      def status_for_table
        status = ""
        status << "<span class='label label-success'>#{I18n.t(:top)}</span> " if self.is_top
        status << "<span class='label label-important'>#{I18n.t(:hot)}</span> " if self.is_hot
        status << "<span class='label'>#{I18n.t(:hidden)}</span>"if self.is_hidden
        status.html_safe
      end
    end

    module ClassMethods
      def with_status(status)
        status = [status].flatten
        self.any_of(status.map{|s| {s.to_sym=>true}})
      end
    end

  end

end