deprecatedが出ていたngminからng-annotateへ切り替え

AngularJSのminify対策をしてくれるアレ。
例によって、generator-angularで作ったプロジェクトでの話。
grunt-ng-annotateをインストールしてgrunt-ngminを外して、記述をngminからngAnnotateにしただけでいけました。

% npm install grunt-ng-annotate -D
diff --git a/package.json b/package.json
index 77190a9..e1f2b1a 100644
--- a/ngapp/package.json
+++ b/ngapp/package.json
@@ -25,7 +25,7 @@
     "grunt-google-cdn": "^0.4.0",
     "grunt-karma": "^0.8.3",
     "grunt-newer": "^0.7.0",
-    "grunt-ngmin": "^0.0.3",
+    "grunt-ng-annotate": "^0.3.2",
     "grunt-protractor-runner": "^1.1.0",
     "grunt-svgmin": "^0.4.0",
     "grunt-usemin": "^2.1.1",
-- a/Gruntfile.coffee
+++ b/Gruntfile.coffee
@@ -300,10 +300,7 @@ module.exports = (grunt) ->
           dest: "<%= yeoman.dist %>"
         ]

-  # ngmin tries to make the code safe for minification automatically by
-  # using the Angular long form for dependency injection. It doesn't work on
-  # things like resolve or inject so those have to be done manually.
-    ngmin:
+    ngAnnotate:
       dist:
         files: [
           expand: true
@@ -443,7 +440,7 @@ module.exports = (grunt) ->
     "concurrent:dist"
     "autoprefixer"
     "concat"
-    "ngmin"
+    "ngAnnotate"
     "copy:dist"
     "cdnify"
     "cssmin"

CoffeeScriptでAngularJSのテスト書いている時に暗黙的に最後の行を returnする仕様に気をつける

まんまこれ↓
CoffeeScript で AngularJS のテストを書くときに気をつけること - Qiita

'use strict'

describe 'Service: AwesomeInterceptor', ->

  httpProvider = {}
  # load the service's module
  beforeEach module 'myApp',($httpProvider)->
    httpProvider= $httpProvider
    return # <- つけた

#.........

Karma-Jasmineの出力結果表示形式をRspec風にしたい

f:id:yuichi_katahira:20140827112945p:plain

そっけないです.....

karma-spec-reporterをインストールして設定する。

% npm install karma-spec-reporter -D
# karma.conf.coffee
  plugins: [
       'karma-phantomjs-launcher'
       'karma-jasmine'
       'karma-coffee-preprocessor'
+     'karma-spec-reporter'
       'karma-coverage'
     ]

-     reporters: ['progress','coverage']
+    reporters: ['spec','coverage']

f:id:yuichi_katahira:20140827113013p:plain

OK.

Railsのwrap_parametersは何をしてくれるのか?

Action Controller Overview — Ruby on Rails Guides
↑ここに書いてある。

config/initializers/wrap_parameters.rbはRails4.1だとデフォルトで多分こうなっている。↓

 Be sure to restart your server when you modify this file.

# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.

# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
  wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
end

# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
#  self.include_root_in_json = true
# end

この設定のままの場合は要するに'application/json'でJSONRailsへ送った時にルート要素を省いて送れる。

//↓これが
{ "user": { "name": "katahirado", "address": "Kakyouin" } }
//↓こう送れる
{ "name": "katahirado", "address": "Kakyouin" }

↓Controllerへ送るとパラメータはこうなる

{ name: "katahirado", address: "Kakyouin", user: { name: "katahirado", address: "Kakyouin" }}

何が嬉しいのかというと、例えばAngularJSとかからJSONを送るときに$resourceで処理しやすい、でもって、Rails側では通常と変わらずUser.new(params[:user])と出来る*1、とかそういう感じで。

ちなみにController名にしたがってラップされるので、そこは注意が必要。
例えばDeviseのRegistrationsControllerの場合は、registration: {...}とラップされてる。

DeviseはJSON考慮に入れてないので、APIとかの用途の場合はSorceryとか、自前とか、oAuthの仕組みに乗るとかしたほうが良さ気。

*1:説明の為にStrongParametersを省いてます

Yeomanのgenerate-angularで作成したプロジェクト(CoffeeScript)にKarma-Coverageを導入する

ココを見ましょう。

ありがとうございます。

やった内容

// package.json

 "karma-coverage": "git+https://github.com/mokemokechicken/karma-coverage.git#master",
% npm l
#karma.conf.js
       'karma-phantomjs-launcher'
       'karma-jasmine'
       'karma-coffee-preprocessor'
+      'karma-coverage'
     ]

# ............................................

-    preprocessors: '**/*.coffee': ['coffee']
+    preprocessors:
+      'app/scripts/*.coffee': 'coffee'
+      'app/scripts/*/**/*.coffee': 'coverage'
+      'test/**/*.coffee': 'coffee'
 
     # Uncomment the following lines if you are using grunt's server to run the tests
     # proxies: '/': 'http://localhost:9000/'
     # URL root prevent conflicts with the site root
     # urlRoot: '_karma_'
+
+    reporters: ['progress','coverage']

OK.そのうち解決されることを願いつつ。

参考

Vagrant環境でProtractorで使うSeleniumをstandalone serverにした

generator-angularで作ったプロジェクトにProtractorでのe2eテストを入れたやつをVagrant環境でアレする。

コレの続き

https://github.com/exratione/protractor-selenium-server-cookbook:このクックブックを追加してよしなに設定する。

で、node_modules/protractor/bin/webdriver-manager updateいらないと思うので消す。

# Gruntfile.coffee
module.exports = (grunt) ->
  grunt.initConfig
# ................................省略.................................

  # E2E test
    protractor:
      options:
        keepAlive: true
        noColor: false
      coffee:
        configFile: "test/protractor.conf.js"
#------------------------削除ここから---------------------
    exec:
      webdriverUpdate: "node_modules/protractor/bin/webdriver-manager update"
#------------------------削除ここまで----------------------
# ................................省略.................................
  grunt.registerTask "test", (target) ->
    if target is "unit"
      grunt.task.run([
        "clean:server"
        "concurrent:test"
        "autoprefixer"
        "connect:test"
        "karma"
      ])
    else if target is "e2e"
      grunt.task.run([
        "clean:server"  #<- "exec:webdriverUpdate"の記述を削除した
        "concurrent:test"
        "autoprefixer"
        "connect:test"
        "protractor"
      ])
    else
      grunt.task.run([
        "clean:server"
        "concurrent:test"
        "autoprefixer"
        "connect:test"
        "karma"
        "protractor"
      ])

参考

Yeomanのgenerator-angularで作ったプロジェクトをVagrant環境で開発する場合の設定

Yeomanで作ったプロジェクト関係なくて、単にVagrant環境で開発する時にホスト側のブラウザとかで確認したい時の話。

こういう場合にgrunt-contrib-connectの設定で、こういうのを見かける↓

#Gruntfile.coffee
     connect: 
       options: 
         port: 9000
         livereload: 35729
         # change this to '0.0.0.0' to access the server from outside
         # hostname: 'localhost'
         hostname: '192.168.x.x'

これだと、ゲスト側のIPアドレスを覚えていないといけない。

ので、こうした。

#Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.hostname = "hostname"
  config.vm.box = "some/box"
  config.vm.network :private_network, ip: "192.168.x.x"
  config.vm.network "forwarded_port", guest: 3000, host: 3000
  config.vm.network "forwarded_port", guest: 9000, host: 9000
  config.vm.network "forwarded_port", guest: 35729, host: 35729
  # ........
#Gruntfile.coffee
   connect:
      options:
        port: 9000
      # Change this to '0.0.0.0' to access the server from outside.
        hostname: "0.0.0.0"
        livereload: 35729

これでホスト側でlocalhost:9000にアクセスすればOK。
192.168.x.x:9000でもアクセス出来る。
ライブリロードも効く。NFS使ってるとライブリロードがちょっと遅いのが難点。そこだけどうにかしたい。

後、Gruntfileでhostをlocalhostと設定していたところは127.0.0.1に修正しといた。 ちなみにUbuntuのイメージ使ってる。