如何从固定装置填充导轨中的表格?

发布于 2024-07-20 06:58:12 字数 782 浏览 4 评论 0原文

快速摘要: 我有一个 Rails 应用程序,它是个人清单/待办事项列表。 基本上,您可以登录并管理您的待办事项列表。

我的问题: 当用户创建新帐户时,我想用 20-30 个默认待办事项填充他们的清单。 我知道我可以说:

wash_the_car = ChecklistItem.new
wash_the_car.name = 'Wash and wax the Ford F650.'
wash_the_car.user = @new_user
wash_the_car.save!

...repeat 20 times...

但是,我有 20 个 ChecklistItem 行要填充,因此这将是 60 行非常潮湿(又称为不干燥)的代码。 一定有更好的办法。

因此,我想在创建帐户时使用 YAML 文件中的 ChecklistItems 表作为种子。 YAML 文件可以填充我的所有 ChecklistItem 对象。 当创建一个新用户时——砰! -- 预设的待办事项在其列表中。

我该怎么做呢?

谢谢!

(PS:对于那些想知道我为什么这样做的人:我正在为我的网页设计公司进行客户端登录。我要经历一组 20 个步骤(第一次会议、设计、验证、测试等)这 20 个步骤是我想要为每个新客户填充的 20 个清单项目。但是,虽然每个人都从相同的 20 个项目开始,但我通常会根据项目自定义要采取的步骤。我的普通待办事项列表实现并希望以编程方式填充行)如果您有疑问,我可以进一步解释。

Quick summary:
I have a Rails app that is a personal checklist / to-do list. Basically, you can log in and manage your to-do list.

My Question:
When a user creates a new account, I want to populate their checklist with 20-30 default to-do items. I know I could say:

wash_the_car = ChecklistItem.new
wash_the_car.name = 'Wash and wax the Ford F650.'
wash_the_car.user = @new_user
wash_the_car.save!

...repeat 20 times...

However, I have 20 ChecklistItem rows to populate, so that would be 60 lines of very damp (aka not DRY) code. There's gotta be a better way.

So I want to use seed the ChecklistItems table from a YAML file when the account is created. The YAML file can have all of my ChecklistItem objects to be populated. When a new user is created -- bam! -- the preset to-do items are in their list.

How do I do this?

Thanks!

(PS: For those of you wondering WHY I am doing this: I am making a client login for my web design company. I have a set of 20 steps (first meeting, design, validate, test, etc.) that I go through with each web client. These 20 steps are the 20 checklist items that I want to populate for each new client. However, while everyone starts with the same 20 items, I normally customize the steps I'll take based on the project (and hence my vanilla to-do list implementation and desire to populate the rows programatically). If you have questions, I can explain further.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很糊涂小朋友 2024-07-27 06:58:12

只需写一个函数:

def add_data(data, user)
wash_the_car = ChecklistItem.new
wash_the_car.name = data
wash_the_car.user = user
wash_the_car.save!
end

add_data('Wash and wax the Ford F650.', @user)

Just write a function:

def add_data(data, user)
wash_the_car = ChecklistItem.new
wash_the_car.name = data
wash_the_car.user = user
wash_the_car.save!
end

add_data('Wash and wax the Ford F650.', @user)
随心而道 2024-07-27 06:58:12

我同意其他回答者的建议,建议您只需用代码来完成即可。 但它不必像建议的那样冗长。 如果您希望它是这样的话,它已经是一个衬垫:

@new_user.checklist_items.create! :name => 'Wash and wax the Ford F650.'

将其放入从文件中读取的项目循环中,或存储在类中或其他任何地方:

class ChecklistItem < AR::Base
  DEFAULTS = ['do one thing', 'do another']
  ...
end

class User < AR::Base
  after_create :create_default_checklist_items

  protected
  def create_default_checklist_items
    ChecklistItem::DEFAULTS.each do |x|
      @new_user.checklist_items.create! :name => x
    end
  end
end

或者如果您的项目复杂性增加,请将字符串数组替换为哈希数组...

# ChecklistItem...
DEFAULTS = [
  { :name => 'do one thing', :other_thing => 'asdf' },
  { :name => 'do another', :other_thing => 'jkl' },
]

# User.rb in after_create hook:    
ChecklistItem::DEFAULTS.each do |x|
  @new_user.checklist_items.create! x
end

但我并不是真的建议您将所有默认值放入 ChecklistItem 内的常量中。 我只是这样描述,以便您可以看到 Ruby 对象的结构。 相反,将它们放入您读取一次并缓存的 YAML 文件中:

class ChecklistItem < AR::Base
  def self.defaults
    @@defaults ||= YAML.read ...
  end
end

或者,如果您希望管理员能够动态管理默认选项,请将它们放入数据库中:

class ChecklistItem < AR::Base
  named_scope :defaults, :conditions => { :is_default => true }
end

# User.rb in after_create hook:    
ChecklistItem.defaults.each do |x|
  @new_user.checklist_items.create! :name => x.name
end

很多选项。

I agree with the other answerers suggesting you just do it in code. But it doesn't have to be as verbose as suggested. It's already a one liner if you want it to be:

@new_user.checklist_items.create! :name => 'Wash and wax the Ford F650.'

Throw that in a loop of items that you read from a file, or store in your class, or wherever:

class ChecklistItem < AR::Base
  DEFAULTS = ['do one thing', 'do another']
  ...
end

class User < AR::Base
  after_create :create_default_checklist_items

  protected
  def create_default_checklist_items
    ChecklistItem::DEFAULTS.each do |x|
      @new_user.checklist_items.create! :name => x
    end
  end
end

or if your items increase in complexity, replace the array of strings with an array of hashes...

# ChecklistItem...
DEFAULTS = [
  { :name => 'do one thing', :other_thing => 'asdf' },
  { :name => 'do another', :other_thing => 'jkl' },
]

# User.rb in after_create hook:    
ChecklistItem::DEFAULTS.each do |x|
  @new_user.checklist_items.create! x
end

But I'm not really suggesting you throw all the defaults in a constant inside ChecklistItem. I just described it that way so that you could see the structure of the Ruby object. Instead, throw them in a YAML file that you read in once and cache:

class ChecklistItem < AR::Base
  def self.defaults
    @@defaults ||= YAML.read ...
  end
end

Or if you wand administrators to be able to manage the default options on the fly, put them in the database:

class ChecklistItem < AR::Base
  named_scope :defaults, :conditions => { :is_default => true }
end

# User.rb in after_create hook:    
ChecklistItem.defaults.each do |x|
  @new_user.checklist_items.create! :name => x.name
end

Lots of options.

黯淡〆 2024-07-27 06:58:12

Rails Fixture 用于填充单元测试的测试数据; 不要认为它应该用于您提到的场景。

我想说的是,只需提取一个新方法 add_checklist_item 即可完成。

def on_user_create
  add_checklist_item 'Wash and wax the Ford F650.', @user
  # 19 more invocations to go
end

如果您想要更大的灵活性

def on_user_create( new_user_template_filename )
  #read each line from file and call add_checklist_item
end

该文件可以是一个简单的文本文件,其中每一行对应一个任务描述,例如“清洗福特 F650 并打蜡。”。 用 Ruby 编写应该很容易,

A Rails Fixture is used to populate test-data for unit tests ; Dont think it's meant to be used in the scenario you mentioned.

I'd say just Extract a new method add_checklist_item and be done with it.

def on_user_create
  add_checklist_item 'Wash and wax the Ford F650.', @user
  # 19 more invocations to go
end

If you want more flexibility

def on_user_create( new_user_template_filename )
  #read each line from file and call add_checklist_item
end

The file can be a simple text file where each line corresponds to a task description like "Wash and wax the Ford F650.". Should be pretty easy to write in Ruby,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文