English 中文(简体)
铁路中令人满意的观点逻辑
原标题:Refactoring view logic in Rails

在这方面,我需要做些什么。 我有<代码>Tournament模型,与有关。 用户,通过<代码>编码>。

添加的“密码>是签字状态。 导游有起步时间,用户只能在比赛开始前60分钟登记。 之后,注册用户可以进行核对。 因此,基本上我有两种选择:国家

简言之,模型看着这一点。

class Signup < ActiveRecord::Base
  REGISTERED = 0
  CHECKED = 1

  belongs_to :tournament
  belongs_to :user
end

class Tournament < ActiveRecord::Base
  has_many :signups
  has_many :users, :through => :signups
end

class User < ActiveRecord::Base  
  has_many :signups
  has_many :tournaments, :through => :signups
end

我用一些法典来保持这一缺陷。 问题在于,因为我有许多条件要牢记。 这里是我的实际代码(,将Slim用作一个排时发动机)。

- if logged_in?
  - if current_user.registered_for?(@tournament)
    - if @tournament.starts_at < 60.minutes.from_now
      p Signups are closed, only registered users can now check in
      - if current_user.registered_for?(@tournament)
        = button_to "Checkin!", { :controller => :signups, :action => :update, :id => @tournament.id }, :method => :put
    - else
      = button_to "Cancel your registration for the tournament", { :controller => :signups, :action => :destroy, :id => @tournament.id }, :method => :delete
  - elsif current_user.checked_in?(@tournament)
    p You have already checked in.            
  - elsif @tournament.starts_at > 60.minutes.from_now
    = button_to "Sign up for the tournament", :controller => :signups, :action => :create, :method => :post, :id => @tournament.id
  - else
    p
      | The tournament starts in less than 60 minutes, you can t sign in
- else
  p 
    | You need to 
    |  
    = link_to "log in", login_path
    |  to play

问题在于,我不知道如何使这一点更加清洁。 我指的是,我能为纽芬兰语增加助手,但是,如果是的话,如果是,则会给我带来帮助,因为有许多不同的组合。 简称:

  • user isn t logged in
  • it s over 60 until the tournament starts and user hasn t yet registered for the tournament
  • it s over 60 until the tournament starts and user is already registered
  • it s under 60 minutes, but user isn t registered yet
  • it s under 60 minutes and user is registered but hasn t checked in
  • it s under 60 minutes and user already checked in

And this is just the tip of the iceberg, because admins should see more information than a regular user, but I don t want to complicate this question.

主要问题是,我如何处理类似案件? 这样做似乎非常可怕,但我看不出任何其他更简单的方式。

最佳回答

更清洁的方法是就你的模式制定有意义的方法。 例如,在你的旅游模式中,添加了如下内容:

def can_register?( user )
  !user.registered_for?(self) && self.starts_at > 60.minutes.from_now
end

那么,你可以先检查<代码>can_register?。 把逻辑与你一样,并不是MVC申请的意图。

问题回答

你们应当利用物体来总结逻辑。 也许可以这样说:

class UserSignup

  def initialize(user, tournament)
    @user, @tournament = user, tournament
  end

  def registered?
    @user.registered_for?(@tournament)
  end

  def signups_closed?
    @tournament.start_at < 1.hour.from_now
  end

  def checked_in?
    @user.checked_in?(@tournament)
  end

end

这使得观点更加简单,不需要做大量工作。 阁下认为,将消除许多重复现象,而且你可以测试你的签名逻辑,独立于这一观点。

你还可以让一位讲演者参加,这比你更有意义,但更确切地说了你们的看法。 参看draper等地段,以帮助您做到这一点。

class SignupPresenter

  def initialize(user_signup)
    @user_signup = user_signup
  end

  def register_button
    view.button_to("sign up") if @user_signup.registered?
  end

  # etc ...

end

此外,我将考虑为不同的用户使用不同的模板,甚至使用控制器。 因此,没有签名的用户根本无法进入这一页,而行政部门则有不同的控制者(甚至名称空间)。

I wouldn t just go in and split it into partials, because that would just hide the logic. I also like a separate object more than putting it into a model, because this way the model doesn t get cluttered as much and all the logic stays together, nicely focussed.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签