angular_sprinkles 0.1.1 → 0.2.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: 04613ad621c5fa4d0bbae54eb75509fee2c95d1f
4
- data.tar.gz: 47b85137b16c7dcebcad026d3c04a1d403d3b59d
3
+ metadata.gz: 5b64d257dda0cf1e8a99f220b37cd4038d4cf8cf
4
+ data.tar.gz: 4a6c9bc4529978427aff6c3b4ba8e14b997bd7fd
5
5
  SHA512:
6
- metadata.gz: ce0ee1de35c5ace45a90ec6b66edc1b1571a0777e7a04e1826604e7fd2bedaab722a7ed6b26a47185b61e7eaafad781477addcd68c9505b6e5af2a99a9f8024d
7
- data.tar.gz: e8e92bcd7ea5c0fbd03c54b9cd8e1bacb1464ce35fa6edb4e7d77beb337ae08370e0dd5b3bce14d09e9c29178bf31e218bbee99f344881101477e90fa40c92d5
6
+ metadata.gz: d198024dbbfba69d739051c5df36717df41a62eab9792b794da9dfcdad20b35557e2fcf809ec9d4832c3c44b361a43e789cd56d4fc65cdcbf83483abd557e264
7
+ data.tar.gz: 2159d526dde0559420cf6144e025b1fbaa5b2a057156153d11dafa507d833bc21ce030a76d9d26dc40900550e3ab3027b791c1c773906e6dbd6e7013882d00a4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.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.1.1 ruby lib
5
+ # stub: angular_sprinkles 0.2.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "angular_sprinkles"
9
- s.version = "0.1.1"
9
+ s.version = "0.2.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-03"
14
+ s.date = "2014-10-10"
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 = [
@@ -34,19 +34,24 @@ Gem::Specification.new do |s|
34
34
  "lib/angular_sprinkles/content_cache.rb",
35
35
  "lib/angular_sprinkles/content_yielder.rb",
36
36
  "lib/angular_sprinkles/context.rb",
37
- "lib/angular_sprinkles/controller.rb",
38
37
  "lib/angular_sprinkles/counter.rb",
39
38
  "lib/angular_sprinkles/directive/attributes.rb",
40
39
  "lib/angular_sprinkles/directive/html.rb",
41
40
  "lib/angular_sprinkles/directive/input.rb",
42
41
  "lib/angular_sprinkles/directive/name.rb",
43
42
  "lib/angular_sprinkles/engine.rb",
43
+ "lib/angular_sprinkles/form_binder.rb",
44
+ "lib/angular_sprinkles/form_binder/base.rb",
45
+ "lib/angular_sprinkles/form_binder/check_box.rb",
46
+ "lib/angular_sprinkles/form_binder/default.rb",
44
47
  "lib/angular_sprinkles/helpers.rb",
45
48
  "lib/angular_sprinkles/helpers/bind_helper.rb",
46
49
  "lib/angular_sprinkles/helpers/directive_helper.rb",
47
50
  "lib/angular_sprinkles/helpers/service_helper.rb",
48
51
  "lib/angular_sprinkles/java_script.rb",
49
52
  "lib/angular_sprinkles/key_generator.rb",
53
+ "lib/angular_sprinkles/mixins/controller.rb",
54
+ "lib/angular_sprinkles/mixins/form_builder.rb",
50
55
  "lib/angular_sprinkles/model_decorator.rb",
51
56
  "lib/angular_sprinkles/object_key_wrapper.rb",
52
57
  "lib/angular_sprinkles/railtie.rb",
@@ -79,7 +84,8 @@ Gem::Specification.new do |s|
79
84
  "spec/dummy/app/models/concerns/.keep",
80
85
  "spec/dummy/app/models/test_model.rb",
81
86
  "spec/dummy/app/views/layouts/application.html.erb",
82
- "spec/dummy/app/views/test/new.html.erb",
87
+ "spec/dummy/app/views/test/form_helpers.html.erb",
88
+ "spec/dummy/app/views/test/javascript_bindings.html.erb",
83
89
  "spec/dummy/bin/bundle",
84
90
  "spec/dummy/bin/rails",
85
91
  "spec/dummy/bin/rake",
@@ -110,7 +116,8 @@ Gem::Specification.new do |s|
110
116
  "spec/dummy/public/422.html",
111
117
  "spec/dummy/public/500.html",
112
118
  "spec/dummy/public/favicon.ico",
113
- "spec/features/javascript_spec.rb",
119
+ "spec/features/form_helpers_spec.rb",
120
+ "spec/features/javascript_bindings_spec.rb",
114
121
  "spec/rails_helper.rb",
115
122
  "spec/spec_helper.rb"
116
123
  ]
@@ -7,15 +7,23 @@ require "angular_sprinkles/directive/input"
7
7
  require "angular_sprinkles/directive/name"
8
8
  require "angular_sprinkles/directive/html"
9
9
  require "angular_sprinkles/context"
10
- require "angular_sprinkles/controller"
11
10
  require "angular_sprinkles/counter"
12
11
  require "angular_sprinkles/engine"
12
+ require "angular_sprinkles/form_binder"
13
+ require "angular_sprinkles/form_binder/base"
14
+ require "angular_sprinkles/form_binder/check_box"
15
+ require "angular_sprinkles/form_binder/default"
13
16
  require "angular_sprinkles/helpers/bind_helper"
14
17
  require "angular_sprinkles/helpers/directive_helper"
15
18
  require "angular_sprinkles/helpers/service_helper"
16
19
  require "angular_sprinkles/helpers"
17
20
  require "angular_sprinkles/java_script"
18
21
  require "angular_sprinkles/key_generator"
22
+ require "angular_sprinkles/mixins/controller"
23
+ require "angular_sprinkles/mixins/form_builder"
19
24
  require "angular_sprinkles/model_decorator"
20
25
  require "angular_sprinkles/object_key_wrapper"
21
26
  require "angular_sprinkles/railtie"
27
+
28
+ ActionController::Base.send(:include, AngularSprinkles::Mixins::Controller)
29
+ ActionView::Helpers::FormBuilder.send(:include, AngularSprinkles::Mixins::FormBuilder)
@@ -0,0 +1,14 @@
1
+ module AngularSprinkles
2
+ class FormBinder
3
+ def self.new(args)
4
+ helper = args.fetch(:helper)
5
+
6
+ case helper
7
+ when :check_box
8
+ CheckBox.new(args)
9
+ else
10
+ Default.new(args)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ module AngularSprinkles
2
+ class FormBinder
3
+ class Base
4
+ def initialize(_args)
5
+ @method = _args.fetch(:method)
6
+ @args = _args.fetch(:args).dup
7
+
8
+ object = _args.fetch(:object)
9
+ attribute = @args.first.to_sym
10
+
11
+ @attribute_binding = object.bind(attribute)
12
+ end
13
+
14
+ def call
15
+ # Let the original form helper raise the exception
16
+ return @args if helper_is_going_raise_argument_error?
17
+
18
+ args_with_binding
19
+ end
20
+
21
+ private
22
+
23
+ def args_with_binding; raise NotImplementedError; end
24
+
25
+ def helper_is_going_raise_argument_error?
26
+ @args.count < required_parameters.count
27
+ end
28
+
29
+ def required_parameters
30
+ @method.parameters.select { |i| i.first == :req }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ module AngularSprinkles
2
+ class FormBinder
3
+ class CheckBox < Base
4
+ private
5
+
6
+ def args_with_binding
7
+ binding = { 'ng-model' => @attribute_binding }
8
+
9
+ @args.tap do |args|
10
+ if args.many?
11
+ args.second.reverse_merge!(binding)
12
+ else
13
+ args.push(binding)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module AngularSprinkles
2
+ class FormBinder
3
+ class Default < Base
4
+ private
5
+
6
+ def args_with_binding
7
+ binding = { 'ng-model' => @attribute_binding }
8
+
9
+ @args.tap do |args|
10
+ # Assume that optional params default to {}
11
+ # This is a safe assumption for all helpers except check_box
12
+ (@method.arity.abs - args.count).times { args.push({}) }
13
+
14
+ # The last argument is always the one where ng-model should be appended
15
+ args.last.reverse_merge!(binding)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,52 @@
1
+ module AngularSprinkles
2
+ module Mixins
3
+ module Controller
4
+ def initialize(*args)
5
+ counter = Counter.new
6
+ key_generator = KeyGenerator.new(counter: counter)
7
+
8
+ constructor_collection = ConstructorCollection.new({
9
+ json_wrapper: JavaScript::ConstructorThis,
10
+ object_wrapper: ObjectKeyWrapper
11
+ })
12
+
13
+ constructor = Constructor.new({
14
+ json_wrapper: JavaScript::ConstructorWrapper,
15
+ collection: constructor_collection
16
+ })
17
+
18
+ @_sprinkles = Context.new({
19
+ key_generator: key_generator,
20
+ constructor: constructor,
21
+ constructor_collection: constructor_collection
22
+ })
23
+
24
+ super
25
+ end
26
+
27
+ def bindable(object)
28
+ object = object.to_a if object.is_a?(::ActiveRecord::Relation)
29
+ object.map!(&method(:bindable)) if object.is_a?(Array)
30
+ key = @_sprinkles.key_generator.call(object)
31
+ @_sprinkles.constructor_collection.push(object, key)
32
+ ModelDecorator.new({
33
+ object: object,
34
+ key: key,
35
+ json_wrapper: JavaScript::BindVariable,
36
+ object_wrapper: ObjectKeyWrapper
37
+ })
38
+ end
39
+
40
+ def view_context
41
+ super.tap do |context|
42
+ @_sprinkles.content_yielder = ContentYielder.new({
43
+ cache: ContentCache.new,
44
+ context: context,
45
+ yield_to: JavaScript::PREFIX
46
+ })
47
+ @_sprinkles.content_yielder.call(@_sprinkles.constructor)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ module AngularSprinkles
2
+ module Mixins
3
+ DEFAULT_HELPERS = ActionView::Helpers::FormBuilder.field_helpers - [:label, :fields_for]
4
+ # DATE_HELPERS = [:date_select, :time_select, :datetime_select]
5
+ # SELECT_HELPERS = [:select, :collection_select, :grouped_collection_select, :time_zone_select, :collection_check_boxes, :collection_radio_buttons]
6
+ # ALL_HELPERS = [*DATE_HELPERS, *SELECT_HELPERS, *DEFAULT_HELPERS]
7
+
8
+ module FormBuilder
9
+ DEFAULT_HELPERS.each do |helper|
10
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
11
+ def bind_#{helper}(*args, &block)
12
+ params = { helper: :#{helper}, method: method(:#{helper}), args: args, object: @object }
13
+ args_with_binding = AngularSprinkles::FormBinder.new(params).call
14
+ #{helper}(*args_with_binding, &block)
15
+ end
16
+ RUBY_EVAL
17
+ end
18
+ end
19
+ end
20
+ end
@@ -5,7 +5,7 @@ describe TestController do
5
5
 
6
6
  let(:name) { "name" }
7
7
 
8
- subject { get :new, { name: name }}
8
+ subject { get :javascript_bindings, { name: name }}
9
9
 
10
10
  it "renders the view with the correct bindings" do
11
11
  model_name = "TestModel_1"
@@ -1,5 +1,10 @@
1
1
  class TestController < ApplicationController
2
- def new
2
+ before_filter :set_model
3
+
4
+ def javascript_bindings; end
5
+ def form_helpers; end
6
+
7
+ def set_model
3
8
  @model = bindable(TestModel.new(name: params[:name]))
4
9
  end
5
10
  end
@@ -0,0 +1,26 @@
1
+ <%= form_for @model, url: '' do |f| %>
2
+ <%# working 100%! %>
3
+ <% [:text_field, :password_field, :hidden_field, :file_field, :text_area,
4
+ :check_box, :color_field, :search_field, :telephone_field,
5
+ :phone_field, :date_field, :time_field, :datetime_field, :datetime_local_field,
6
+ :month_field, :week_field, :url_field, :email_field, :number_field, :range_field].each do |helper| %>
7
+
8
+ <%= f.send("bind_#{helper}", :name) %>
9
+ <% end %>
10
+
11
+ <%= f.bind_radio_button :name, :cool %>
12
+
13
+ <%# still need to check [:collection_check_boxes, :collection_radio_buttons] %>
14
+
15
+ <%# bind method works, but binding not correctly hooking up %>
16
+ <%# <%= f.bind_select :name %1> %>
17
+ <%# <%= f.bind_date_select :name %1> %>
18
+ <%# <%= f.bind_time_select :name %1> %>
19
+ <%# <%= f.bind_datetime_select :name %1> %>
20
+ <%# <%= f.bind_time_zone_select :name, nil, include_blank: true %1> %>
21
+
22
+ <%# doesn't work at all %>
23
+ <%# <%= f.bind_collection_select :name, :id, [], :id, :name %1> %>
24
+ <%# <%= f.bind_grouped_collection_select :name %1> %>
25
+
26
+ <% end %>
@@ -15,10 +15,3 @@
15
15
  <label for="bind">Bind</label>
16
16
  <input id="bind" name="bind" type="text" ng-model="<%= bind(:hello, :world) %>" />
17
17
  </div>
18
-
19
- <%# Rails bug with form_for and simple delegator. %>
20
- <%# Add this here to test the work around. %>
21
- <%= form_for @model, url: '' do |f| %>
22
- <%= f.label :name %>
23
- <%= f.text_field :name %>
24
- <% end %>
@@ -1,5 +1,8 @@
1
1
  Rails.application.routes.draw do
2
- root 'test#new'
2
+ root 'test#javascript_bindings'
3
+
4
+ get 'javascript_bindings', to: 'test#javascript_bindings'
5
+ get 'form_helpers', to: 'test#form_helpers'
3
6
  # The priority is based upon order of creation: first created -> highest priority.
4
7
  # See how all your routes lay out with "rake routes".
5
8
 
@@ -0,0 +1,15 @@
1
+ require "rails_helper"
2
+
3
+ # javascript specs are slow, so test everything at
4
+ # once on a single page
5
+
6
+ feature "form_helpers", js: true do
7
+ let(:name) { 'name' }
8
+ let(:new_name) { 'namenamename?' }
9
+
10
+ it 'renders all of the helpers with ng-model' do
11
+ expect {
12
+ visit form_helpers_path(name: name)
13
+ }.not_to raise_error
14
+ end
15
+ end
@@ -3,12 +3,12 @@ require "rails_helper"
3
3
  # javascript specs are slow, so test everything at
4
4
  # once on a single page
5
5
 
6
- feature "javascript", js: true do
6
+ feature "javascript_bindings", js: true do
7
7
  let(:name) { 'name' }
8
8
  let(:new_name) { 'namenamename?' }
9
9
 
10
10
  before do
11
- visit root_path(name: name)
11
+ visit javascript_bindings_path(name: name)
12
12
  end
13
13
 
14
14
  it "should test all the things" do
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.1.1
4
+ version: 0.2.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-03 00:00:00.000000000 Z
11
+ date: 2014-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -81,19 +81,24 @@ files:
81
81
  - lib/angular_sprinkles/content_cache.rb
82
82
  - lib/angular_sprinkles/content_yielder.rb
83
83
  - lib/angular_sprinkles/context.rb
84
- - lib/angular_sprinkles/controller.rb
85
84
  - lib/angular_sprinkles/counter.rb
86
85
  - lib/angular_sprinkles/directive/attributes.rb
87
86
  - lib/angular_sprinkles/directive/html.rb
88
87
  - lib/angular_sprinkles/directive/input.rb
89
88
  - lib/angular_sprinkles/directive/name.rb
90
89
  - lib/angular_sprinkles/engine.rb
90
+ - lib/angular_sprinkles/form_binder.rb
91
+ - lib/angular_sprinkles/form_binder/base.rb
92
+ - lib/angular_sprinkles/form_binder/check_box.rb
93
+ - lib/angular_sprinkles/form_binder/default.rb
91
94
  - lib/angular_sprinkles/helpers.rb
92
95
  - lib/angular_sprinkles/helpers/bind_helper.rb
93
96
  - lib/angular_sprinkles/helpers/directive_helper.rb
94
97
  - lib/angular_sprinkles/helpers/service_helper.rb
95
98
  - lib/angular_sprinkles/java_script.rb
96
99
  - lib/angular_sprinkles/key_generator.rb
100
+ - lib/angular_sprinkles/mixins/controller.rb
101
+ - lib/angular_sprinkles/mixins/form_builder.rb
97
102
  - lib/angular_sprinkles/model_decorator.rb
98
103
  - lib/angular_sprinkles/object_key_wrapper.rb
99
104
  - lib/angular_sprinkles/railtie.rb
@@ -126,7 +131,8 @@ files:
126
131
  - spec/dummy/app/models/concerns/.keep
127
132
  - spec/dummy/app/models/test_model.rb
128
133
  - spec/dummy/app/views/layouts/application.html.erb
129
- - spec/dummy/app/views/test/new.html.erb
134
+ - spec/dummy/app/views/test/form_helpers.html.erb
135
+ - spec/dummy/app/views/test/javascript_bindings.html.erb
130
136
  - spec/dummy/bin/bundle
131
137
  - spec/dummy/bin/rails
132
138
  - spec/dummy/bin/rake
@@ -157,7 +163,8 @@ files:
157
163
  - spec/dummy/public/422.html
158
164
  - spec/dummy/public/500.html
159
165
  - spec/dummy/public/favicon.ico
160
- - spec/features/javascript_spec.rb
166
+ - spec/features/form_helpers_spec.rb
167
+ - spec/features/javascript_bindings_spec.rb
161
168
  - spec/rails_helper.rb
162
169
  - spec/spec_helper.rb
163
170
  homepage: http://github.com/BrewhouseTeam/angular_sprinkles
@@ -1,52 +0,0 @@
1
- module AngularSprinkles
2
- module Controller
3
- def initialize(*args)
4
- counter = Counter.new
5
- key_generator = KeyGenerator.new(counter: counter)
6
-
7
- constructor_collection = ConstructorCollection.new({
8
- json_wrapper: JavaScript::ConstructorThis,
9
- object_wrapper: ObjectKeyWrapper
10
- })
11
-
12
- constructor = Constructor.new({
13
- json_wrapper: JavaScript::ConstructorWrapper,
14
- collection: constructor_collection
15
- })
16
-
17
- @_sprinkles = Context.new({
18
- key_generator: key_generator,
19
- constructor: constructor,
20
- constructor_collection: constructor_collection
21
- })
22
-
23
- super
24
- end
25
-
26
- def bindable(object)
27
- object = object.to_a if object.is_a?(::ActiveRecord::Relation)
28
- object.map!(&method(:bindable)) if object.is_a?(Array)
29
- key = @_sprinkles.key_generator.call(object)
30
- @_sprinkles.constructor_collection.push(object, key)
31
- ModelDecorator.new({
32
- object: object,
33
- key: key,
34
- json_wrapper: JavaScript::BindVariable,
35
- object_wrapper: ObjectKeyWrapper
36
- })
37
- end
38
-
39
- def view_context
40
- super.tap do |context|
41
- @_sprinkles.content_yielder = ContentYielder.new({
42
- cache: ContentCache.new,
43
- context: context,
44
- yield_to: JavaScript::PREFIX
45
- })
46
- @_sprinkles.content_yielder.call(@_sprinkles.constructor)
47
- end
48
- end
49
- end
50
- end
51
-
52
- ActionController::Base.send(:include, AngularSprinkles::Controller)