mustache_form 0.1.3 → 0.2.2

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: 1e7a3e7f0799ba94da894f6fa0840cf3494a80d4
4
- data.tar.gz: 2fe8498eb18012c4df44f885fd71af0f3957273d
3
+ metadata.gz: 4a634f7998badd9cb4bf7ca76be09ac2b928ca72
4
+ data.tar.gz: 1a9b57331d0c95076edcade87e5ece415b46f796
5
5
  SHA512:
6
- metadata.gz: b889fb68a570fe1d1316627555ab77d72b85795a13244bf8050bf38ac36dc77a67bf9fb67e933a786beaa735f6b8f87fab0c6210da8310ecbbab38df89f4f56f
7
- data.tar.gz: a4094992cd5c36796c783eec395932fa12a0618d5387cf9aa0f3f5c1000f0cf0e2e726184687a0a09879ec7a7982951959388d6008eb73df4234c681239870ee
6
+ metadata.gz: ba6faa080c298d0a6de9b3a6b38d7c0e33397fa23eb5d882785445bf726d00d254689c2ed97c8e2043bcdf090627e3e34d37d51407e5bb81c94805ddd7a51f47
7
+ data.tar.gz: 79363d7486b4ee22330c489f6572dd45fdb2076c6d07323232af437abb275748f3161538b3441901b5d49f157c507fc34a2e16a3a674af1d657944409e357b44
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # MustacheForm
2
2
 
3
+ ## Builds
4
+ [![Build Status](https://travis-ci.org/netflakes/mustache_form.svg?branch=master)](https://travis-ci.org/netflakes/mustache_form)
5
+ [![Coverage Status](https://coveralls.io/repos/netflakes/mustache_form/badge.svg)](https://coveralls.io/r/netflakes/mustache_form)
6
+
3
7
  This is a small library that adds a set of helper methods to Mustache view classes. Mustache allows complete
4
8
  seperation of concerns with view templates. With Mustache, the typical view layer is split up into two sub-layers: a Ruby Class-based "view" and an HTML "template", so you can concentrate on crafting your HTML without polluting it with embedded Ruby logic. This separation also makes it easier to test your view-code. The helper module has wrapper methods for the basic rails form_tag and form_for helpers. The current version also has wrappers for the simple_form form_helpers as well.
5
9
 
@@ -21,6 +25,23 @@ Or install it yourself as:
21
25
 
22
26
  $ gem install mustache_form
23
27
 
28
+
29
+ ## Installation - Configuration
30
+
31
+ Mustache form has an install generator that creates an initializer allowing you to turn on and off
32
+ options. For now the only option is to enable the SimpleForm support globally. Basically this means
33
+ that when this option is turned on - then the simple_form helper will always be used.
34
+
35
+
36
+ ```
37
+ rails generate mustache_form:install
38
+ ```
39
+
40
+
41
+ You can safely turn this on if you are using simple_form as you can also turn it off for individual
42
+ forms, see below:
43
+
44
+
24
45
  ## Usage
25
46
 
26
47
  The best way to explain how to use this little gem is with a few code samples from an existing
@@ -73,7 +94,7 @@ module People
73
94
  include Wrapper::People::Form
74
95
 
75
96
  def labeled_simple_form
76
- custom_simple_form_for(@person, url: {controller: "people", action: "create"}) do |f|
97
+ mustache_simple_form_for(@person, url: {controller: "people", action: "create"}) do |f|
77
98
  simple_form_common(f)
78
99
  end
79
100
  end
@@ -124,10 +145,26 @@ module People
124
145
 
125
146
  ```
126
147
 
127
- #### NB: simple_form gem dependancy
148
+ #### SimpleForm - Simple Fields For
149
+
150
+ **Simple Form** also comes with some extra helpers you can use inside rails default forms without relying
151
+ on `simple_form_for` helper. They are listed below.
152
+
153
+ Wrapper to use **Simple Form** inside a default rails form. It works in the same way that the `fields_for`
154
+ Rails helper, but change the builder to use the `SimpleForm::FormBuilder`.
155
+
156
+ ```ruby
157
+ form_for @user do |f|
158
+ f.simple_fields_for :posts do |posts_form|
159
+ # Here you have all simple_form methods available
160
+ posts_form.input :title
161
+ end
162
+ end
163
+ ```
128
164
 
129
- This version has an inbuilt dependancy on the simple_form gem. I intend to remove this dependancy in the next
130
- build and include an install generator that lets the user decide whether it should be included and enabled or not.
165
+ **Mustache Form** should support these as we are simply yielding to your view method that contains the inputs
166
+ during the process and hence you should be able to use these - though I would caution that I have not had the
167
+ chance to test this feature.
131
168
 
132
169
  ## Development
133
170
 
data/Rakefile CHANGED
@@ -1,6 +1,13 @@
1
1
  require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
2
+ require 'rspec/core/rake_task'
3
+ #
4
4
  RSpec::Core::RakeTask.new(:spec)
5
-
6
5
  task :default => :spec
6
+ #
7
+ desc 'Run the test suite for ruby scripts'
8
+ namespace :spec do
9
+ task :all do
10
+ Rake::Task["spec"].reenable
11
+ Rake::Task["spec"].invoke
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module MustacheForm
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copy MustacheForm initializer"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def copy_config
8
+ template "config/initializers/mustache_form.rb"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ #
2
+ MustacheForm.setup do |config|
3
+ #
4
+ # - by default simple_form is not enabled
5
+ #
6
+ config.simple_form_enabled = false
7
+ end
data/lib/mustache_form.rb CHANGED
@@ -4,15 +4,21 @@ require File.dirname(__FILE__) + '/mustache_form/form_helper'
4
4
  require File.dirname(__FILE__) + '/mustache_form/railtie' if defined? ::Rails::Railtie
5
5
  #
6
6
  module MustacheForm
7
- class Config
8
- attr_accessor :simple_form_enabled
9
- end
10
7
 
11
- def self.config
12
- @@config ||= Config.new
8
+ @@configured = false
9
+
10
+ def self.configured?
11
+ @@configured
13
12
  end
14
13
 
15
- def self.configure
16
- yield self.config
14
+ # - the configuration option to turn on simple_form support or off (in the initializer)
15
+ mattr_accessor :simple_form_enabled
16
+ @@error_method = false
17
+
18
+ # Default way to setup MustacheForm. Run rails generate mustache_form:install
19
+ # to create a fresh initializer with all configuration values.
20
+ def self.setup
21
+ @@configured = true
22
+ yield self if block_given?
17
23
  end
18
24
  end
@@ -1,62 +1,60 @@
1
1
  require 'mustache'
2
2
  #
3
+ # When the Mustache variable is a callable object, such as a function or lambda, the
4
+ # object will be invoked and passed the block of text. hence we then yield to the
5
+ # standard ruby form_for to build our form and then have Mustache render each key value pair
3
6
  module MustacheForm
4
7
  module FormHelper
5
8
 
6
- def custom_form_tag(url: nil, html: nil)
7
- # When the Mustache variable is a callable object, such as a function or lambda, the
8
- # object will be invoked and passed the block of text. hence we then yield to the
9
- # standard ruby form_for to build our form and then have Mustache render each key value pair
9
+ # The basic form_tag just wraps the rails form_tag helper and then yields to
10
+ # the view method that handles the form inputs
11
+ #
12
+ #-form_tag(url_for_options = {}, options = {}, &block)
13
+ #
14
+ def mustache_form_tag(url_for_options: {}, html_options: {})
15
+ formable = :form_tag
10
16
  lambda do |text|
11
- form_tag(url: url, html: html) do |f|
17
+ send(formable, url_for_options, html_options) do |f|
12
18
  obj = FormedMustache.new(yield(f))
13
19
  Mustache.render(text, obj).html_safe
14
20
  end
15
21
  end
16
22
  end
17
23
 
18
- def custom_form_for(object, url: nil, html: nil)
19
- # When the Mustache variable is a callable object, such as a function or lambda, the
20
- # object will be invoked and passed the block of text. hence we then yield to the
21
- # standard ruby form_for to build our form and then have Mustache render each key value pair
24
+ # The form_for version in the basic form wraps the rails form_for helper and
25
+ # then yields to the method that handles the form inputs. It also handles the
26
+ # the optional use of the simple_form form helper. Since the method signature
27
+ # is identical it just needs to call the correct method name
28
+ #
29
+ #-form_for(record, options = {}, &block)
30
+ #-simple_form_for(record, options = {}, &block)
31
+ #
32
+ def mustache_form_for(object, url_for_options: {}, html_options: {}, use_rails_form_helper: false)
33
+ MustacheForm.simple_form_enabled == true ? formable = :simple_form_for : formable = :form_for
34
+ formable = :form_for if use_rails_form_helper
22
35
  lambda do |text|
23
- form_for(object, url: url, html: html) do |f|
36
+ options = {
37
+ url: url_for_options, html: html_options
38
+ }
39
+ send(formable, object, options) do |f|
24
40
  obj = FormedMustache.new(yield(f))
25
41
  Mustache.render(text, obj).html_safe
26
42
  end
27
43
  end
28
44
  end
29
45
 
30
- def custom_simple_form_tag(url: nil, html: nil)
31
- # When the Mustache variable is a callable object, such as a function or lambda, the
32
- # object will be invoked and passed the block of text. hence we then yield to the
33
- # standard ruby form_for to build our form and then have Mustache render each key value pair
34
- lambda do |text|
35
- simple_form_tag(url: url, html: html) do |f|
36
- obj = FormedMustache.new(yield(f))
37
- Mustache.render(text, obj).html_safe
38
- end
46
+ def self.included(base)
47
+ base.class_eval do
48
+ alias_method :custom_form_tag, :mustache_form_tag
49
+ alias_method :custom_form_for, :mustache_form_for
39
50
  end
40
51
  end
41
52
 
42
- def custom_simple_form_for(object, url: nil, html: nil)
43
- # When the Mustache variable is a callable object, such as a function or lambda, the
44
- # object will be invoked and passed the block of text. hence we then yield to the
45
- # standard ruby form_for to build our form and then have Mustache render each key value pair
46
- lambda do |text|
47
- simple_form_for(object, url: url, html: html) do |f|
48
- obj = FormedMustache.new(yield(f))
49
- Mustache.render(text, obj).html_safe
50
- end
51
- end
52
- end
53
53
  end
54
- #
55
- class FormedMustache < Mustache
56
54
 
55
+ class FormedMustache < Mustache
57
56
  def initialize(data)
58
57
  data.each_pair do |key, value|
59
- #puts "\n\nFormedMustache k:#{key}, v: #{value}\n\n"
60
58
  FormedMustache.send(:define_method, key, proc{value})
61
59
  end
62
60
  end
@@ -1,19 +1,14 @@
1
1
  #
2
2
  module MustacheForm
3
3
  class Railtie < Rails::Railtie
4
- #
5
- # enable namespaced configuration in Rails environments
6
- config.mustache_form = ActiveSupport::OrderedOptions.new
7
- #
8
- initializer :after_initialize do |app|
9
- #
10
- MustacheForm.configure do |config|
11
- config.simple_form_enabled = app.config.mustache_form[:simple_form_enabled]
4
+ config.eager_load_namespaces << MustacheForm
5
+
6
+ config.after_initialize do
7
+ unless MustacheForm.configured?
8
+ warn 'Mustache Form is not configured in the application and will use the default values.' +
9
+ ' Use `rails generate mustache_form:install` to generate the Mustache Form configuration.'
12
10
  end
13
- #
14
- # dynamically load the client rails app module that has the stuff
15
- #LoadLists.load
16
- end
11
+ end
17
12
  end
18
13
  end
19
14
  #
@@ -1,3 +1,3 @@
1
1
  module MustacheForm
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustache_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Forkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-30 00:00:00.000000000 Z
11
+ date: 2015-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -181,6 +181,8 @@ files:
181
181
  - Rakefile
182
182
  - bin/console
183
183
  - bin/setup
184
+ - lib/generators/mustache_form/install_generator.rb
185
+ - lib/generators/mustache_form/templates/config/initializers/mustache_form.rb
184
186
  - lib/mustache_form.rb
185
187
  - lib/mustache_form/form_helper.rb
186
188
  - lib/mustache_form/railtie.rb