重构基于用户的模块化网站

发布于 2024-07-21 22:25:34 字数 484 浏览 9 评论 0原文

我正在使用 Ruby on Rails、MySQL 和 Javascript 开发一个网站。

该网站是模块化的,这意味着用户可以自定义其主页并将一个模块拖放到其所在的另一个区域。 有三列可以放下模块。 (想想 iGoogle 或 Netvibes)

存储这些数据的最佳方式是什么,以便用户返回主页时可以快速重建?

我考虑过这样做,每个模块都有一个与用户相对应的 ID、它所在的行以及它在该行中的位置。 (所以像.. user|column|row 这样的东西,将等于 1204|3|27,这意味着对于用户 #1204,该模块将位于第 3 列和距顶部 27 个空格的位置。

然后,当用户返回时,只需循环遍历 添加 1,直到到达该列的末尾,然后从另一列开始,直到填充所有 3 列,

但我觉得这会非常慢,并且必须有更好的方法来做到这一点。

,向每一列 ?主要是寻找数据库结构,但如果你有一些与此相关的 Rails 代码,我不介意查看 git。

I am developing a website in Ruby on Rails, MySQL, and Javascript.

The website is modular, meaning that a user can customize their homepage and drag and drop one module to another area where it will live. there are three columns where the module can be dropped. (Think iGoogle or Netvibes)

What is the best way to store this data, so that when the user returns their homepage can be reconstructed quickly?

I've considered doing it in a way where each module get's an ID that corresponds to the user, it's row and it's positiong in that row. (So something like.. user|column|row, would equal 1204|3|27, meaning that for user #1204 this module would be in column #3 and 27 spaces from the top.

Then when the user returns, just loop through, adding 1 to each until it reaches the end of the column and start again at the other one until all 3 columns are populated.

I feel like this would be very slow though, and there must be a better way to do it.

Any suggestions? Mostly looking for Database structure, but if you have some Rails code that would corrilate to this I wouldn't mind seein git.

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

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

发布评论

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

评论(2

内心旳酸楚 2024-07-28 22:25:34

我认为最好的方法是将数据保存在单个文本字段中

,例如 1204|3|27 应该保存在文本字段中...如果每个用户 ID 有一个良好的索引,您应该非常非常快地获得配置。 之后,您只需“分解”“|”的配置即可 。

问候

I think the best is to keep your data in a single text field

For example 1204|3|27 should be in a text field ... with a good index per user id you should get the configuration very very fast. After that you just need to "explode" your configuration for "|" .

Regards

当梦初醒 2024-07-28 22:25:34

我说建模非常简单:

class ModuleInstallation < AR::Base
  belongs_to :user
  belongs_to :module
  validates_presence_of :column
  validates_presence_of :row
end

class User < AR::Base
  has_many :module_installations, :order => :column
  has_many :modules, :through => :module_installations
end

然后让你的控制器处理更多的排序,像这样(未经测试,但查看它以及我用来获取概念的方法的文档):

@columns = current_user.module_installations.group_by(&:column)
@columns.each { |column, modules| modules.sort! { |x,y| x.row <=> y.row } }

然后你的视图相对简单:

<% @columns.each do |column, modules| %>
  <div class="column-<%= column %>">
    <% modules.each do |module| %>
      <div class="module">
        <%= module.to_html %>
      </div>
    <% end %>
  </div>
<% end %>

I say model it very straightforwardly:

class ModuleInstallation < AR::Base
  belongs_to :user
  belongs_to :module
  validates_presence_of :column
  validates_presence_of :row
end

class User < AR::Base
  has_many :module_installations, :order => :column
  has_many :modules, :through => :module_installations
end

Then let your controller handle any more sorting, something like this (untested, but look through it and the documentation for the methods I'm using to get the concepts):

@columns = current_user.module_installations.group_by(&:column)
@columns.each { |column, modules| modules.sort! { |x,y| x.row <=> y.row } }

Then your view is relatively simple:

<% @columns.each do |column, modules| %>
  <div class="column-<%= column %>">
    <% modules.each do |module| %>
      <div class="module">
        <%= module.to_html %>
      </div>
    <% end %>
  </div>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文