angular_sprinkles 0.2.14 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dff2c0fdcd7737a0adcce73fb1ce61386b3c0b42
4
- data.tar.gz: d333af388f0a7ccfbe56f3824e8336b01a5cacf6
3
+ metadata.gz: d2ca75f5bc2cc8cdd98f994baec30cccf119ab8c
4
+ data.tar.gz: 39b1c5dfaff4c05c824ec7372573e74010b0474c
5
5
  SHA512:
6
- metadata.gz: 3cacd67c49ae9ea243664e5ba9b40bc5a8192a6a38767e12f99c9d10b3c6bf4478ea200a7e9d2f87ee69499ef9be9eca221039219cdeddc5e9233ca0a1f002af
7
- data.tar.gz: 3b5557d45d3b8904e28ecb59929829e7ea5090c1fa5d8afec07a8c043b9d033e14bd01ab02f74b135b3af94b91cb1f08eb9cfd0ce1eef0712b09a74528988090
6
+ metadata.gz: 2cb89f9dcca04611c218c3057d2bfd23cc29862e06ac51f9da227ed54bb29a553fafc7ace16add21bccdb89f83e2d91c7762c47895d36385ed31e06d6e79f678
7
+ data.tar.gz: dd63bd3d015ac50937a6aeebca8e143e382849264ceaf69d800ec72113b013921da62e5fd73cdfc3eddc810f2ace3da09d48a1f3631b0aa760ccc20fe7d730ca
data/README.md CHANGED
@@ -42,6 +42,7 @@ Include and `angular_sprinkles` into your `application.js`.
42
42
 
43
43
  - [Two-way binding](#two-way-binding)
44
44
  - [Directives](#directives)
45
+ - [Controllers and Isolate Scopes](#controllers-and-isolate-scopes)
45
46
  - [Inlining function calls](#inlining-function-calls)
46
47
  - [Form helpers](#form-helpers)
47
48
 
@@ -79,6 +80,60 @@ sprinkles.directive('blink', function () {
79
80
  <% end %>
80
81
  ```
81
82
 
83
+ Directives can also return it's controller to a ruby block if the directive uses transclusion.
84
+
85
+ ```erb
86
+ <script>
87
+ sprinkles.directive('someDirective', function () {
88
+ return {
89
+ transclude: true,
90
+ template: '<div ng-transclude></div>',
91
+ // note: 'controllerAs' must be the directive name + 'Ctrl'
92
+ controllerAs: 'someDirectiveCtrl',
93
+ controller: function () {
94
+ this.alertMe = function (name) {
95
+ alert('Hi, ' + name);
96
+ };
97
+ }
98
+ }
99
+ });
100
+ </script>
101
+
102
+ <%= directive(:someDirective) do |some_ctrl| %>
103
+ <button ng-click="<%= some_ctrl.call('Gabe') %>">CLICK ME!</button>
104
+ <% end %>
105
+ ```
106
+
107
+ ### Controllers and Isolate Scopes
108
+
109
+ If you would rather skip the directive and just create a controller, there is the `ctrl` helper.
110
+
111
+ ```erb
112
+ <script>
113
+ sprinkles.controller('someCtrl', function () {
114
+ this.alertMe = function (name) {
115
+ alert('Hi, ' + name);
116
+ };
117
+ });
118
+ </script>
119
+
120
+ <%= ctrl(:someCtrl) do |some_ctrl| %>
121
+ <button ng-click="<%= some_ctrl.call('Gabe') %>">CLICK ME!</button>
122
+ <% end %>
123
+ ```
124
+
125
+ This is good for localizing JavaScript behavior. Additionally, if you'd just like to create a new
126
+ scope, you can use the `isolate` helper which creates an "anonymous" controller to wrap your element.
127
+
128
+ ```erb
129
+ <%= isolate do |iso_ctrl| %>
130
+ <input ng-model="<%= iso_ctrl.bind(:isolated_binding) %>">
131
+ {{ <%= iso_ctrl.bind(:isolated_binding) %> }}
132
+ <% end %>
133
+ ```
134
+
135
+ In contrast with the `bind` helper, bindings made here do not apply to the scope of the root controller.
136
+
82
137
  ### Inlining function calls
83
138
 
84
139
  Call services directly from the view with the `service` helper.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.14
1
+ 0.3.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: angular_sprinkles 0.2.14 ruby lib
5
+ # stub: angular_sprinkles 0.3.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "angular_sprinkles"
9
- s.version = "0.2.14"
9
+ s.version = "0.3.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Gabe Scholz"]
14
- s.date = "2014-10-19"
14
+ s.date = "2014-10-22"
15
15
  s.description = "Add a few sprinkles of AngularJS to your Rails App"
16
16
  s.email = "gabe@brewhouse.io"
17
17
  s.extra_rdoc_files = [
@@ -35,11 +35,10 @@ Gem::Specification.new do |s|
35
35
  "lib/angular_sprinkles/content_yielder.rb",
36
36
  "lib/angular_sprinkles/context.rb",
37
37
  "lib/angular_sprinkles/counter.rb",
38
- "lib/angular_sprinkles/directive/attributes.rb",
39
- "lib/angular_sprinkles/directive/controller.rb",
40
- "lib/angular_sprinkles/directive/html.rb",
41
- "lib/angular_sprinkles/directive/input.rb",
42
- "lib/angular_sprinkles/directive/name.rb",
38
+ "lib/angular_sprinkles/element/attributes.rb",
39
+ "lib/angular_sprinkles/element/html.rb",
40
+ "lib/angular_sprinkles/element/input.rb",
41
+ "lib/angular_sprinkles/element/scope.rb",
43
42
  "lib/angular_sprinkles/engine.rb",
44
43
  "lib/angular_sprinkles/form_binder/base.rb",
45
44
  "lib/angular_sprinkles/form_binder/check_box.rb",
@@ -47,7 +46,10 @@ Gem::Specification.new do |s|
47
46
  "lib/angular_sprinkles/helpers.rb",
48
47
  "lib/angular_sprinkles/helpers/bind_form_for_helper.rb",
49
48
  "lib/angular_sprinkles/helpers/bind_helper.rb",
49
+ "lib/angular_sprinkles/helpers/controller_helper.rb",
50
50
  "lib/angular_sprinkles/helpers/directive_helper.rb",
51
+ "lib/angular_sprinkles/helpers/element_helper.rb",
52
+ "lib/angular_sprinkles/helpers/isolate_helper.rb",
51
53
  "lib/angular_sprinkles/helpers/service_helper.rb",
52
54
  "lib/angular_sprinkles/java_script.rb",
53
55
  "lib/angular_sprinkles/key_generator.rb",
@@ -62,11 +64,10 @@ Gem::Specification.new do |s|
62
64
  "spec/angular_sprinkles/content_yielder_spec.rb",
63
65
  "spec/angular_sprinkles/context_spec.rb",
64
66
  "spec/angular_sprinkles/counter_spec.rb",
65
- "spec/angular_sprinkles/directive/attributes_spec.rb",
66
- "spec/angular_sprinkles/directive/controller_spec.rb",
67
- "spec/angular_sprinkles/directive/html_spec.rb",
68
- "spec/angular_sprinkles/directive/input_spec.rb",
69
- "spec/angular_sprinkles/directive/name_spec.rb",
67
+ "spec/angular_sprinkles/element/attributes_spec.rb",
68
+ "spec/angular_sprinkles/element/html_spec.rb",
69
+ "spec/angular_sprinkles/element/input_spec.rb",
70
+ "spec/angular_sprinkles/element/scope_spec.rb",
70
71
  "spec/angular_sprinkles/form_binder/base_spec.rb",
71
72
  "spec/angular_sprinkles/form_binder/check_box_spec.rb",
72
73
  "spec/angular_sprinkles/form_binder/default_spec.rb",
@@ -2,11 +2,10 @@ require "angular_sprinkles/cache"
2
2
  require "angular_sprinkles/constructor"
3
3
  require "angular_sprinkles/constructor_collection"
4
4
  require "angular_sprinkles/content_yielder"
5
- require "angular_sprinkles/directive/attributes"
6
- require "angular_sprinkles/directive/controller"
7
- require "angular_sprinkles/directive/input"
8
- require "angular_sprinkles/directive/name"
9
- require "angular_sprinkles/directive/html"
5
+ require "angular_sprinkles/element/attributes"
6
+ require "angular_sprinkles/element/html"
7
+ require "angular_sprinkles/element/input"
8
+ require "angular_sprinkles/element/scope"
10
9
  require "angular_sprinkles/context"
11
10
  require "angular_sprinkles/counter"
12
11
  require "angular_sprinkles/engine"
@@ -15,7 +14,10 @@ require "angular_sprinkles/form_binder/check_box"
15
14
  require "angular_sprinkles/form_binder/default"
16
15
  require "angular_sprinkles/helpers/bind_form_for_helper"
17
16
  require "angular_sprinkles/helpers/bind_helper"
17
+ require "angular_sprinkles/helpers/controller_helper"
18
18
  require "angular_sprinkles/helpers/directive_helper"
19
+ require "angular_sprinkles/helpers/element_helper"
20
+ require "angular_sprinkles/helpers/isolate_helper"
19
21
  require "angular_sprinkles/helpers/service_helper"
20
22
  require "angular_sprinkles/helpers"
21
23
  require "angular_sprinkles/java_script"
@@ -1,5 +1,5 @@
1
1
  module AngularSprinkles
2
- module Directive
2
+ module Element
3
3
  class Attributes
4
4
  def initialize(args, options)
5
5
  @args = args
@@ -1,5 +1,5 @@
1
1
  module AngularSprinkles
2
- module Directive
2
+ module Element
3
3
  class Html
4
4
  def initialize(args)
5
5
  @args = (args || {}).symbolize_keys
@@ -1,5 +1,5 @@
1
1
  module AngularSprinkles
2
- module Directive
2
+ module Element
3
3
  class Input
4
4
  def initialize(args)
5
5
  @args = (args || {})
@@ -1,19 +1,19 @@
1
1
  module AngularSprinkles
2
- module Directive
3
- class Controller
2
+ module Element
3
+ class Scope
4
4
  def initialize(args)
5
- @name = args.fetch(:name)
5
+ @base = args.fetch(:base)
6
6
  @object_wrapper = args.fetch(:object_wrapper)
7
7
  @bind_json_wrapper = args.fetch(:bind_json_wrapper)
8
8
  @call_json_wrapper = args.fetch(:call_json_wrapper)
9
9
  end
10
10
 
11
11
  def bind(attribute = nil)
12
- @object_wrapper.new(@name, attribute, @bind_json_wrapper)
12
+ @object_wrapper.new(@base, attribute, @bind_json_wrapper)
13
13
  end
14
14
 
15
15
  def call(function, *input)
16
- @object_wrapper.new(@name, function, input, @call_json_wrapper)
16
+ @object_wrapper.new(@base, function, input, @call_json_wrapper)
17
17
  end
18
18
  end
19
19
  end
@@ -2,7 +2,10 @@ module AngularSprinkles
2
2
  module Helpers
3
3
  include BindFormForHelper
4
4
  include BindHelper
5
+ include ControllerHelper
5
6
  include DirectiveHelper
7
+ include ElementHelper
8
+ include IsolateHelper
6
9
  include ServiceHelper
7
10
  end
8
11
  end
@@ -0,0 +1,13 @@
1
+ module AngularSprinkles
2
+ module Helpers
3
+ module ControllerHelper
4
+ def ctrl(controller_name, opts = {}, &block)
5
+ options = opts.dup.symbolize_keys
6
+
7
+ options['ng-controller'] = "#{controller_name} as #{controller_name}"
8
+ options[:scope_name] ||= controller_name
9
+ _element(options, &block)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,25 +1,12 @@
1
1
  module AngularSprinkles
2
2
  module Helpers
3
3
  module DirectiveHelper
4
- def directive(directive_name, options = {}, &block)
5
- if block_given?
6
- controller = Directive::Controller.new({
7
- name: "#{directive_name}Ctrl",
8
- object_wrapper: ObjectKeyWrapper,
9
- bind_json_wrapper: JavaScript::NoOp,
10
- call_json_wrapper: JavaScript::BindService
11
- })
4
+ def directive(directive_name, opts = {}, &block)
5
+ options = opts.dup.symbolize_keys
12
6
 
13
- content = capture(controller, &block)
14
- end
15
-
16
- name = Directive::Name.new(directive_name)
17
- input = Directive::Input.new(options.except(:html))
18
- html = Directive::Html.new(options[:html])
19
-
20
- attributes = Directive::Attributes.new([name, input, html], tag: html.tag, content: content)
21
-
22
- content_tag(*attributes.to_content_tag)
7
+ options[directive_name] = ""
8
+ options[:scope_name] ||= "#{directive_name}Ctrl"
9
+ _element(options, &block)
23
10
  end
24
11
  end
25
12
  end
@@ -0,0 +1,31 @@
1
+ module AngularSprinkles
2
+ module Helpers
3
+ module ElementHelper
4
+ def _element(opts = {}, &block)
5
+ options = opts.dup.symbolize_keys
6
+
7
+ scope_name = options.delete(:scope_name)
8
+ html_options = options.delete(:html) || {}
9
+ input_options = options # use remaining options
10
+
11
+ if block_given?
12
+ scope = Element::Scope.new({
13
+ base: scope_name,
14
+ object_wrapper: ObjectKeyWrapper,
15
+ bind_json_wrapper: JavaScript::NoOp,
16
+ call_json_wrapper: JavaScript::BindService
17
+ })
18
+
19
+ content = capture(scope, &block)
20
+ end
21
+
22
+ input = Element::Input.new(input_options)
23
+ html = Element::Html.new(html_options)
24
+
25
+ attributes = Element::Attributes.new([input, html], tag: html.tag, content: content)
26
+
27
+ content_tag(*attributes.to_content_tag)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ module AngularSprinkles
2
+ module Helpers
3
+ module IsolateHelper
4
+ def isolate(opts = {}, &block)
5
+ controller_name = "isolate_#{SecureRandom.hex}"
6
+ isolate_controller = ObjectKeyWrapper.new(controller_name, JavaScript::IsolateController)
7
+
8
+ @_sprinkles.content_yielder.call(isolate_controller)
9
+ ctrl(controller_name, opts, &block)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -81,5 +81,9 @@ module AngularSprinkles
81
81
  NoOp = ->(*args) do
82
82
  args.flatten.compact.join('.')
83
83
  end
84
+
85
+ IsolateController = ->(name) do
86
+ "sprinkles.controller('#{name}', angular.noop);"
87
+ end
84
88
  end
85
89
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe AngularSprinkles::Directive::Attributes do
3
+ describe AngularSprinkles::Element::Attributes do
4
4
  let(:something) { double(attributes: {}) }
5
5
  let(:name) { something }
6
6
  let(:input) { something }
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe AngularSprinkles::Directive::Html do
3
+ describe AngularSprinkles::Element::Html do
4
4
  let(:args) { { key: 'value', tag: 'a' } }
5
5
 
6
6
  subject { described_class.new(args) }
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe AngularSprinkles::Directive::Input do
3
+ describe AngularSprinkles::Element::Input do
4
4
  let(:args) { { "key" => value } }
5
5
 
6
6
  subject { described_class.new(args) }
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe AngularSprinkles::Directive::Controller do
4
- let(:name) { double }
3
+ describe AngularSprinkles::Element::Scope do
4
+ let(:base) { double }
5
5
  let(:object_wrapper) { double }
6
6
  let(:bind_json_wrapper) { double }
7
7
  let(:call_json_wrapper) { double }
8
8
 
9
9
  subject do
10
10
  described_class.new({
11
- name: name,
11
+ base: base,
12
12
  object_wrapper: object_wrapper,
13
13
  bind_json_wrapper: bind_json_wrapper,
14
14
  call_json_wrapper: call_json_wrapper
@@ -16,23 +16,23 @@ describe AngularSprinkles::Directive::Controller do
16
16
  end
17
17
 
18
18
  describe "#bind" do
19
- it "wraps the controller name" do
19
+ it "wraps the controller base" do
20
20
  attribute = double
21
21
 
22
- expect(object_wrapper).to receive(:new).with(name, attribute, bind_json_wrapper)
22
+ expect(object_wrapper).to receive(:new).with(base, attribute, bind_json_wrapper)
23
23
  subject.bind(attribute)
24
24
 
25
- expect(object_wrapper).to receive(:new).with(name, nil, bind_json_wrapper)
25
+ expect(object_wrapper).to receive(:new).with(base, nil, bind_json_wrapper)
26
26
  subject.bind
27
27
  end
28
28
  end
29
29
 
30
30
  describe "#call" do
31
- it "wraps the controller name" do
31
+ it "wraps the controller base" do
32
32
  function = double
33
33
  input = [double, double]
34
34
 
35
- expect(object_wrapper).to receive(:new).with(name, function, input, call_json_wrapper)
35
+ expect(object_wrapper).to receive(:new).with(base, function, input, call_json_wrapper)
36
36
  subject.call(function, *input)
37
37
  end
38
38
  end
@@ -47,3 +47,7 @@ sprinkles.directive('nestedDirective', function () {
47
47
  template: '<h2>{{name}}</h2>'
48
48
  };
49
49
  });
50
+
51
+ sprinkles.controller('someCtrl', function () {
52
+ this.someValue = 42;
53
+ });
@@ -9,6 +9,15 @@
9
9
  <%= directive(:nested_directive, name: @model.bind(:name), html: { tag: 'h1' }) %>
10
10
  <% end %>
11
11
 
12
+ <%= ctrl(:someCtrl, html: { id: "ctrl-div" }) do |some_ctrl| %>
13
+ {{ <%= some_ctrl.bind(:someValue) %> }}
14
+ <% end %>
15
+
16
+ <%= isolate do |some_ctrl| %>
17
+ <div ng-init="<%= some_ctrl.bind(:someValue) %> = 42"></div>
18
+ <div id="isolate-div" ng-bind="<%= some_ctrl.bind(:someValue) %>"></div>
19
+ <% end %>
20
+
12
21
  <div id="model-bind">
13
22
  <label for="input">Input</label>
14
23
  <input id="input" name="input" type="text" ng-model="<%= @model.bind(:name) %>" />
@@ -28,5 +28,7 @@ feature "javascript_bindings", js: true do
28
28
  # directive controller results
29
29
  expect(find("#directive-ctrl-bind")).to have_content("bigHelloWorldCtrlAttribute")
30
30
  expect(find("#directive-ctrl-call")).to have_content('func result')
31
+ expect(find("#ctrl-div")).to have_content(42)
32
+ expect(find("#isolate-div")).to have_content(42)
31
33
  end
32
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angular_sprinkles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.14
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabe Scholz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-19 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -82,11 +82,10 @@ files:
82
82
  - lib/angular_sprinkles/content_yielder.rb
83
83
  - lib/angular_sprinkles/context.rb
84
84
  - lib/angular_sprinkles/counter.rb
85
- - lib/angular_sprinkles/directive/attributes.rb
86
- - lib/angular_sprinkles/directive/controller.rb
87
- - lib/angular_sprinkles/directive/html.rb
88
- - lib/angular_sprinkles/directive/input.rb
89
- - lib/angular_sprinkles/directive/name.rb
85
+ - lib/angular_sprinkles/element/attributes.rb
86
+ - lib/angular_sprinkles/element/html.rb
87
+ - lib/angular_sprinkles/element/input.rb
88
+ - lib/angular_sprinkles/element/scope.rb
90
89
  - lib/angular_sprinkles/engine.rb
91
90
  - lib/angular_sprinkles/form_binder/base.rb
92
91
  - lib/angular_sprinkles/form_binder/check_box.rb
@@ -94,7 +93,10 @@ files:
94
93
  - lib/angular_sprinkles/helpers.rb
95
94
  - lib/angular_sprinkles/helpers/bind_form_for_helper.rb
96
95
  - lib/angular_sprinkles/helpers/bind_helper.rb
96
+ - lib/angular_sprinkles/helpers/controller_helper.rb
97
97
  - lib/angular_sprinkles/helpers/directive_helper.rb
98
+ - lib/angular_sprinkles/helpers/element_helper.rb
99
+ - lib/angular_sprinkles/helpers/isolate_helper.rb
98
100
  - lib/angular_sprinkles/helpers/service_helper.rb
99
101
  - lib/angular_sprinkles/java_script.rb
100
102
  - lib/angular_sprinkles/key_generator.rb
@@ -109,11 +111,10 @@ files:
109
111
  - spec/angular_sprinkles/content_yielder_spec.rb
110
112
  - spec/angular_sprinkles/context_spec.rb
111
113
  - spec/angular_sprinkles/counter_spec.rb
112
- - spec/angular_sprinkles/directive/attributes_spec.rb
113
- - spec/angular_sprinkles/directive/controller_spec.rb
114
- - spec/angular_sprinkles/directive/html_spec.rb
115
- - spec/angular_sprinkles/directive/input_spec.rb
116
- - spec/angular_sprinkles/directive/name_spec.rb
114
+ - spec/angular_sprinkles/element/attributes_spec.rb
115
+ - spec/angular_sprinkles/element/html_spec.rb
116
+ - spec/angular_sprinkles/element/input_spec.rb
117
+ - spec/angular_sprinkles/element/scope_spec.rb
117
118
  - spec/angular_sprinkles/form_binder/base_spec.rb
118
119
  - spec/angular_sprinkles/form_binder/check_box_spec.rb
119
120
  - spec/angular_sprinkles/form_binder/default_spec.rb
@@ -1,19 +0,0 @@
1
- module AngularSprinkles
2
- module Directive
3
- class Name
4
- def initialize(*names)
5
- @names = names.flatten.map { |name| name.to_s.underscore.dasherize }
6
- end
7
-
8
- def attributes
9
- { data: to_empty_hash }
10
- end
11
-
12
- private
13
-
14
- def to_empty_hash
15
- Hash[@names.product([''])]
16
- end
17
- end
18
- end
19
- end
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe AngularSprinkles::Directive::Name do
4
- let(:args) { [:first, :second, :third] }
5
-
6
- subject { described_class.new(args) }
7
-
8
- it 'turns the args hash into an empty data hash' do
9
- expect(subject.attributes).to eq({ data: { "first" => '', "second" => '', "third" => '' } })
10
- end
11
- end