有时我想运行bundle更新但只是为了查看哪些gem需要更新.我不一定要处理与更新所有问题相关的问题,但我想快速检查以了解最新技术水平.
有没有办法简单地让Bundler列出需要与我当前运行的版本(理想情况下)一起更新的宝石,以及最新和最好的版本?
我知道Rails观察者不应该直接访问控制器.这是有道理的,没有人知道观察者将从哪个上下文调用.但是我有一个案例,我认为两者之间的间接沟通是值得的,我想知道如何实现它.
记录和编写分析事件
我想使用观察者触发Google Analytics中的某些事件.目前的工作方式是应用程序控制器有一个记录事件的方法,然后application.html.erb模板将相关的javascript打印到页面中:
class ApplicationController < ActionController::Base
def logGAEvent category, action, opt_hash={}
event = { :category => category,
:action => action,
:label => opt_hash[:label],
:value => opt_hash[:value]}
(session[:ga_events] ||= []) << event
end
end
Run Code Online (Sandbox Code Playgroud)
Application.html.erb
<html>
<head>
...
<script type="text/javascript">
<%= print_ga_events_js %>
</script>
</head>
...
</html>
Run Code Online (Sandbox Code Playgroud)
示例事件:
class UsersController < ApplicationController
...
def create
...
if @new_user
logGAEvent('user', 'signup')
end
end
end
Run Code Online (Sandbox Code Playgroud)
为什么我想在观察者和控制器之间进行通信
目前,在某些值得注意的事件(有人注册,创建新的配置文件等)之后,在控制器中调用logGAEvent方法.
将大多数这些事件抽象为观察者将是一种更好的模式.这样可以整理控制器,也可以减少跟踪.但是,一旦他们进入观察者,模板仍然需要一种方法来访问观察者的数据并将其打印出来.
我希望能做什么
由于观察者不应该真正了解控制器,我想要做的是将这些事件记录到一次性缓冲区中,以便在每次调用结束时刷新它们,但控制器也可以访问它们文件:
class UserObserver < ActiveRecord::Observer
after_create user
# I have …Run Code Online (Sandbox Code Playgroud) 我有我的交易电子邮件系统设置,默认情况下,当事件发生时,人们会收到电子邮件:
class Comment
after_create :email_original_poster
def email_original_poster
UserMailer.delay.notify_author_of_comment self
end
end
Run Code Online (Sandbox Code Playgroud)
然而,不是收到电子邮件,我的一大部分用户更喜欢每日或每周摘要.
实现这个最干净最优雅的方法是什么?
我已经delayed_job运行但是这并不像是一项delayed_job工作,因为我正在排队需要执行的数据,而不是需要执行的操作.
......没有重新发明排队系统
我知道明显的解决方案是表格queued_emails,当然我可以做到这一点.我问这个问题的原因是这样做是重新发明排队系统.不仅有很多排队系统,但正如Percona这篇措辞谨慎的帖子所指出的那样,最好不要推销自己的排队系统:
您是否实施了摘要电子邮件,是否使用了delayed_job以及您学到了什么?
我想做一些基本的检查,以确保正确生成XML站点地图,但have_selector似乎无法检测标记:
require 'spec_helper'
describe SitemapController do
render_views
before(:all) do
# code to generate factory data
# ...
end
# illustrating the problem
it "should be able detect nodes that are definitely present" do
get :index
response.should have_selector('url')
end
end
Run Code Online (Sandbox Code Playgroud)
每次运行测试时都会出现以下错误:
RSpec::Expectations::ExpectationNotMetError: expected css "url" to return something
Run Code Online (Sandbox Code Playgroud)
正在生成站点地图,当我调试RSpec测试并查看response对象时,我可以看到xml内容:
ruby-1.9.2-p180 :001 > response.body
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n <url>\n <loc>http://test.host/panel ...
Run Code Online (Sandbox Code Playgroud)
我的站点地图由SitemapController生成,视图位于views/sitemap/index.builder.xml.
为什么没有have_selector踢?
我一直在考虑MVC中的元内容,特别是页面标题和元描述(这对于指导Google在搜索结果中显示的代码段非常有用).
我不能确定这应该在哪里生活.根据(对于UGC应用程序)读者与内容的交互方式,通常会有一些逻辑.
我无法确定是否在视图层或控制器中更好地构造了此元内容.它几乎肯定不会存在于模型中,因为它特定于数据的特定视图,但是当我的第一直觉是将它放在视图中时,我觉得它可能更好地被抽象化.
我对其他人采取的方法很感兴趣.
我刚开始使用bower来管理客户端依赖项.我已经将Bower设置为安装所有文件/vendor/assets/components.
然后我运行bower install评估bower.json文件并安装所有依赖项
#/bower.json
{
"name": "My-App",
"dependencies": {
"angular": "1.0.8",
"bootstrap": "3.0.0"
}
}
Run Code Online (Sandbox Code Playgroud)
最后,按照我读过的教程的指示,我从GIT中删除了组件目录.
#.gitignore
#...
# Ignore all stuff manged by bower
/vendor/assets/components
Run Code Online (Sandbox Code Playgroud)
因此,该项目不会在slug中包含任何这些资产,并且需要bower install运行才能安装它们.
这对我来说似乎是明智的,就像将项目中的实际宝石分离是明智的一样.它还遵循12因素应用程序的原则,并明确声明和隔离依赖项.
但是,当我推送到Heroku时,资产预编译失败,因为尚未添加asssets,所以当sprockets尝试评估时:
#application.css.scss
/* ...
*= require bootstrap/dist/css/bootstrap
*= require_self
*= require_tree .
*/
Run Code Online (Sandbox Code Playgroud)
它发现没有什么可以找到的,bootstrap/dist/css/bootstrap因为凉亭尚未安装任何东西.
我已经按照本教程建议添加一个package.json包含以下内容的文件:
"dependencies": {
"bower": "0.6.x"
},
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install"
}
Run Code Online (Sandbox Code Playgroud)
但是,bower install …
我试图将Fontawesome包含在Rails 4应用程序中,但资产并没有进入资产管道.但是,字体并没有在生产中出现,我无法弄清楚原因.
我的所有资产都存储在这里,/assets/components
以便Fontawesome出现在:( /assets/components/font-awesome它们位于不同的目录中,因为我正在使用Bower).
# application.css.scss
/* ...
*= require bootstrap/dist/css/bootstrap
*= require font-awesome/css/font-awesome
*= require_self
*= require_tree .
*/
Run Code Online (Sandbox Code Playgroud)
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.assets.paths << Rails.root.join('vendor', 'assets', 'components')
# Adding Webfonts to the Asset Pipeline
config.assets.precompile << Proc.new { |path|
if path =~ /\.(eot|svg|ttf|woff|otf)\z/
true
end
}
Run Code Online (Sandbox Code Playgroud)
我添加了预编译指令,以便根据此问题对所有字体进行预编译
#gemfile
group :production do
gem "rails_12factor" …Run Code Online (Sandbox Code Playgroud) 我有一个模型,它依赖于一个单独的连接模型.
class Magazine < ActiveRecord::Base
has_one :cover_image, dependent: :destroy, as: :imageable
end
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
图像是多态的,可以附加到许多对象(页面和文章),而不仅仅是杂志.
该杂志还存储了可用于宣传它的屏幕截图:
class Magazine < ActiveRecord::Base
has_one :cover_image, dependent: :destroy, as: :imageable
has_one :screenshot
def generate_screenshot
# go and create a screenshot of the magazine
end
end
Run Code Online (Sandbox Code Playgroud)
现在,如果图像发生变化,杂志还需要更新其截图.所以杂志真的需要知道什么时候图像发生了什么.
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
after_save { update_any_associated_magazine }
def update_any_associated_magazine
# figure out if this belongs to a magazine and trigger
# screenshot to regenerate …Run Code Online (Sandbox Code Playgroud) 我一直在尝试将一些 Material 图标包含到 Angular 项目中,但每次我都会发现一些特殊之处。
以下方法成功地将所有图标包含到项目中:
<!-- index.html -->
<head>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
...
</head
Run Code Online (Sandbox Code Playgroud)
// app.module
import { MatIconModule } from '@angular/material/icon'
...
@NgModule({
imports: [
MatIconModule
]
})
Run Code Online (Sandbox Code Playgroud)
<!-- component.html -->
<mat-icon aria-hidden="false" aria-label="Example home icon">home</mat-icon>
Run Code Online (Sandbox Code Playgroud)
该图标已包含并按预期显示在页面中。然而,由于我从未明确导入过我正在使用的一个图标,所以我必须假设整个模块已被导入。对于一个图标来说,有很多代码,我更愿意将它们一一导入。
由于包含整个模块会让我的包变得臃肿,所以我尝试使用MatIconRegistry. 我的理解是,这应该允许我一一注册图标。
// app.module
import { MatIconModule } from '@angular/material/icon'
...
@NgModule({
imports: [
MatIconModule
]
})
Run Code Online (Sandbox Code Playgroud)
// my.component.ts
import { DomSanitizer } from '@angular/platform-browser'
import { MatIconRegistry } from '@angular/material/icon'
...
constructor(iconRegistry: …Run Code Online (Sandbox Code Playgroud) 我希望能够读取 Angular PWA 当前活动的哈希值。但是我看不到任何这样做的机制。
SwUpdate 对象提供可观察量,允许您在新版本激活或可用时读取当前版本的哈希值,但似乎没有公开任何用于获取当前版本哈希值的静态方法。
class SwUpdate {
available: Observable<UpdateAvailableEvent>
activated: Observable<UpdateActivatedEvent>
unrecoverable: Observable<UnrecoverableStateEvent>
isEnabled: boolean
checkForUpdate(): Promise<void>
activateUpdate(): Promise<void>
}
Run Code Online (Sandbox Code Playgroud)
我希望能够读取类似SwUpdate.current读取当前哈希值(例如a518f9f1ab65954c2eafa02fec134fa54b391651)的内容,而不必等待available或activated事件。这可能吗?