从current_level计算天数? [英] Counting the Days from the current_level?

查看:166
本文介绍了从current_level计算天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共有5个级别.

每个级别都有一定的天数,必须等到习惯才能升级到下一个级别(按n_days细分):

Each level has a certain amount of days in it that must pass before the habit can move up to the next level (as broken down by n_days):

      case n_days     
          when 0..9
            1
          when 10..24
            2
          when 25..44
            3  #Level 3
          when 45..69
            4
          when 70..99
            5
          else
            "Mastery"
         end
      end

我们如何用类似<%= habit.current_level.n_days.count_off_from_zero_to_show %>之类的东西从现在的习惯指数中调用n_days?

How can we call the n_days from the present level in the habits index with something like <%= habit.current_level.n_days.count_off_from_zero_to_show %>?

例如,如果我们专门位于第3层的50,它将在习惯指数中显示第5天.

For example, if we are specifically at 50 on level 3 it would show Day 5 in the habits index.

habit.rb

class Habit < ActiveRecord::Base
    belongs_to :user
    has_many :comments, as: :commentable
    has_many :levels
    serialize :committed, Array
    validates :date_started, presence: true
    before_save :current_level
    acts_as_taggable
    scope :private_submit, -> { where(private_submit: true) }
    scope :public_submit, -> { where(private_submit: false) }

attr_accessor :missed_one, :missed_two, :missed_three

    def save_with_current_level
        self.levels.build
        self.levels.build
        self.levels.build
        self.levels.build
        self.levels.build
        self.save
    end

    def self.committed_for_today
    today_name = Date::DAYNAMES[Date.today.wday].downcase
    ids = all.select { |h| h.committed.include? today_name }.map(&:id)
    where(id: ids)
  end 

    def current_level_strike
      levels[current_level - 1] # remember arrays indexes start at 0
    end

    def current_level
            return 0 unless date_started
            committed_wdays = committed.map { |day| Date::DAYNAMES.index(day.titleize) }
            n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday } - self.missed_days

      case n_days     
          when 0..9
            1
          when 10..24
            2
          when 25..44
            3
          when 45..69
            4
          when 70..99
            5
          else
            "Mastery"
        end
    end
end

level.rb

class Level < ActiveRecord::Base
  belongs_to :habit
end

模式

create_table "habits", force: true do |t|
  t.integer  "missed_days",    default: 0
  t.text     "committed"
  t.integer  "days_lost",   default: 0
  t.datetime "date_started"
  t.string   "trigger"
  t.string   "target"
  t.string   "reward"
  t.boolean  "private_submit"
  t.integer  "user_id"
  t.datetime "created_at",                 null: false
  t.datetime "updated_at",                 null: false
  t.integer  "order"
end

add_index "habits", ["user_id", "created_at"], name: "index_habits_on_user_id_and_created_at"
add_index "habits", ["user_id"], name: "index_habits_on_user_id"

create_table "levels", force: true do |t|
  t.integer  "habit_id"
  t.integer  "days_lost",   default: 0
  t.integer  "missed_days",   default: 0
  t.integer  "current_level"
  t.datetime "created_at",                null: false
  t.datetime "updated_at",                null: false
end

add_index "levels", ["habit_id"], name: "index_levels_on_habit_id"

其中的要点: https://gist.github.com/RallyWithGalli/c66dee6dfb9ab5d338c2

推荐答案

在您的current_level方法中,您有两条逻辑线最终代表了n_days的值.如果要在该方法或case语句之外重用该值,则应将值存储在实例变量中,或将逻辑移到另一个方法中.如果逻辑仅对此类很重要,那么我可以将其设为私有方法.

In your current_level method you've got two lines of logic that ultimately represent the value of n_days. If you want to reuse that value outside of that method or case statement, you should either store the value in an instance variable, or move the logic into another method. If the logic is only important to this class, then I might make it a private method.

class Habit < ActiveRecord::Base
  # Existing code ... 

  private

  def committed_wdays
    committed.map do |day|    
      Date::DAYNAMES.index(day.titleize)
    end
  end

  def n_days
    ((date_started.to_date)..Date.today).count do |date| 
      committed_wdays.include? date.wday
    end - self.missed_days
  end
end

私有方法可以进一步完善,但是希望这可以使您了解如何继续提取共享逻辑.

The private methods can be further refined, but hopefully this gives you an idea of how you might continue to extract the shared logic.

这篇关于从current_level计算天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆