Tag: 重构

多个before_action调用坏代码样式?

我正在开发一个带有很多before_actions的控制器的应用程序。 它们中的大多数通过它们设置的实例变量彼此连接。 例如: def first_action @first_variable = Something.new end def second_action if @first_variable @second_variable = Other.new end end 控制器看起来像这样: class ExampleController < ApplicationController before_action :first_action, only: [:index, :show, :create] before_action :second_action, only: [:index, :show, :create] before_action :third_action, only: [:index, :show, :create] before_action :fourth_action, only: [:index, :show, :create] before_action :fifth_action, only: [:index, :show, :create] before_action :sixth_action, […]

我如何应用得墨忒耳法?

我有一个公认的丑陋的查询,要找到与当前角色相关的特定角色。 该行产生正确的结果: @person_event_role.event_role.event.event_roles. joins(:mission_role).where(:mission_roles => {:title => ‘Boss’}). first.person_event_roles.first.person (您可以从多个调用中推断出关联) 获取此信息的唯一方法需要大量数据库结构的知识,但要删除耦合…这需要在该链的每个步骤中填充一堆辅助函数以返回所需的信息。 ..

将业务规则转移到模型中

我之前问了一个问题,引起了一些很好的回应。 这是早先的问题 在那里给出的一些建议的背面,我尝试移动以下控制器逻辑 if params[:concept][:consulted_legal] == 0 && params[:concept][:consulted_marketing] == 1 @concept.attributes = {:status => ‘Awaiting Compliance Approval’} elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 1 @concept.attributes = {:status => ‘Awaiting Marketing Approval’} elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 0 @concept.attributes = {:status => ‘Awaiting Marketing & Legal Approval’} else @concept.attributes = {:status => […]

Ruby on rails控制器代码,需要重构最好的方法才能更干燥?

我有一个欢迎wizzard,在首次登录时构建用户个人资料。 问题是它是非常混乱的实现,但我试图重构它几次并重写它,但不能用更好的东西,然后下面。 在理想的世界中,它将全部在welcome_controller.rb中,但是这引起了很大的麻烦所以现在我改写了profile_controller的更新方法。 有关如何改善这一点的任何想法使它更干燥和干净? 是否愿意收到一些关于此的好的意见和想法,或许将所有更新的东西转移到欢迎控制器? WelcomeController: class WelcomeController current_user.id) @profile = Profile.where(:user_id => current_user.id).first @photo = Photo.new if [“photos”, “basics”, “details”, “test”].member?(params[:step]) # force rendering the correct step case current_user.profile.step when 1 render :template => “/profiles/edit/edit_photos”, :layout => “welcome” when 2 render :template => “/profiles/edit/edit_basics”, :layout => “welcome” when 3 render :template => “/profiles/edit/edit_details”, :layout […]

如何动态生成关联名称?

我正在使用Ruby on Rails 3.2.2和Squeel gem。 我有以下语句,我试图在Mixin模块中重构my_squeel_query方法(因为它被我的许多模型使用): # Note: ‘article_comment_associations’ and ‘model_as_like_article_comment_associations’ # refer to database table names. class Article < ActiveRecord::Base def my_squeel_query commenters. .where{ article_comment_associations.article_id.eq(my{self.id}) & … } end end class ModelAsLikeArticle < ActiveRecord::Base def my_squeel_query commenters. .where{ model_as_like_article_comment_associations.article_id.eq(my{self.id}) & … } end end 我的问题是我无法通过在Mixin模块中生成动态名称来重构article_comment_associations和model_as_like_article_comment_associations语句。 也就是说,如果那是一个String我可以通过使用类似”#{self.class.to_s.singularize}_comment_associations”的内容动态生成相关名称,如下所示: class Article < ActiveRecord::Base include MyModule end class […]

rails通过迁移删除旧模型

我有一堆rails模型,我正在重写为单个模型,以简化我的代码并减少不必要的表。 我想知道删除模型类及其表的最佳方法是什么。 我希望过去的迁移仍能成功,但我不想让空模型四处闲置。 我是否必须手动删除引用这些模型的旧迁移,然后手动删除类文件? 有没有人有最好的方法来做到这一点?

使用shoulda重构Rails模型上的rspec测试

通过回答关于属性可访问性测试的另一个StackOverflow问题 (并认为它们非常棒)来了解shoulda-matchers后,我决定尝试重构我在The Rails Tutorial中所做的模型测试,试图使它们更加简洁和彻底。 我这样做归功于来自模块的文档的一些灵感: Shoulda::Matchers::ActiveRecord和Shoulda::Matchers::ActiveModel ,以及这个StackOverflow关于结构化应该在模型中进行测试的答案 。 但是,还有一些我不确定的事情,我想知道如何使这些测试更好。 我将使用Rails教程中的用户规范作为我的示例,因为它是最详细的,并涵盖了许多可以改进的领域。 以下代码示例已从原始user_spec.rb更改,并将代码替换为describe “micropost associations”行。 针对user.rb模型的规范测试及其工厂在factories.rb中定义。 规格/型号/ user_spec.rb # == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # password_digest :string(255) # remember_token :string(255) […]

DRYING rails视图:partial vs helper

我需要有关DRYing视图代码的最佳实践的建议。 我的应用程序中有三个类(NewsItem,RssItem和BlogItem),它们使用不同的视图,但在它们中有相似的部分。 其中一个部分是这样的: 这三个class级几乎相同,所以我决定把它带到一个单独的地方。 在这里我很困惑:我应该使用部分或辅助方法吗? 我知道帮助程序主要用于从HTML中分离ruby代码,但在这种情况下,帮助程序将如下所示: def toolbar_for(item, type_str, edit_path) if current_user content_tag(:footer) do |b| marks_bar(item).to_s << (delete_from_favorite_button(item, type_str) || favorite_button(@item, type_str)).to_s << share_button(@item).to_s << (content_tag(:div) { link_to("Edit", edit_path)} if current_user.is_mine?(@item)).to_s end end end 所以,这里几乎没有HTML代码。 你能不能给我建议,你认为哪种方法更好,为什么? 此外,这些方法中是否存在一些性能问题(例如,多个字符串串联或频繁的部分加载可能代价高昂)? (这个应用程序相当高负载)

使用词典重构Ruby on Rails i18n YAML文件

这个 StackOverflow问题给了我一些关于Rails i18n文件的良好结构的思考,所以我想我会分享另一个结构来重构Rails i18n yml文件供您考虑/批评。 鉴于我想 保持默认的应用程序结构,这样我就可以在我的视图中使用像t(‘.some_translation’)这样的简写“懒惰”查找,并且知道在应用程序中使用翻译的地方, 避免尽可能多的字符串重复,特别是使用不仅相同的单词,但也有相同的上下文/含义, 只需更改一次密钥就可以反映出它引用的所有地方, 对于config / locales / en.yml文件,看起来像这样: activerecord: attributes: user: email: Email name: Name password: Password password_confirmation: Confirmation models: user: User users: fields: email: Email name: Name password: Password confirmation: Confirmation sessions: new: email: Email password: Password 我可以看到存在重大的重复,并且诸如“电子邮件”和“密码”之类的词语的上下文是明确的并且在它们各自的视图中具有相同的含义。 如果我决定将“电子邮件”更改为“电子邮件”,那么必须去更改它们会有点烦人,所以我想重构字符串以引用某种字典。 那么,如何将字典哈希添加到文件的顶部,其中包含一些&锚点: dictionary: email: &email Email name: &name Name password: […]

ruby – 重构if else语句

我已经尝试阅读一些关于重构的教程,我正在努力解决条件问题。 我不想使用三元运算符,但也许这应该在方法中提取? 或者有一种聪明的方式来使用地图吗? detail.stated = if value[:stated].blank? nil elsif value[:stated] == “Incomplete” nil elsif value[:is_ratio] == “true” value[:stated] == “true” else apply_currency_increment_for_save(value[:stated]) end