easy_form 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fce27e92b82d1ba8882cb37b1c6f853dd71e568
4
- data.tar.gz: 515a9cbe2d5fe17753f5f31a34b3999c19614d1a
3
+ metadata.gz: 9e29536c0b9549c789633a3b14ff5bf6d62d13ff
4
+ data.tar.gz: 371f546e9666837efd820bd5feb3f2266b4d2e33
5
5
  SHA512:
6
- metadata.gz: 214cfb0c7c2073a713cf0b9535b70c5fbcd555d03729774a47cd26bdda9b11532c3c1bc18d48e4ef1fe86d9d93162996dc77cb5e38f0d9d777632697e750bdc7
7
- data.tar.gz: ef91a00547c31e682070f69aaa976aeb94b6f471130c87e8892f7bf5bb513c2e253201197af70038c1c32fd26deb48ed37244a70f0c3449d3f50eceb642fcfe4
6
+ metadata.gz: c731dd1ca55343adab51f9ebccff8c969e49eb27940f4dc711fdd9a8398984da111026bee60dfd026763fe0c03b55122b541576380035a72ca63ccffff77b9b6
7
+ data.tar.gz: 211cada54b7fb760fc385056fa1ddca14be5699f3db3855fcdb658086258aa54058632a167d224d241a50f43b07daaf1048ab83291dddde9fb4895432a955a33
data/easy_form.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'easy_form/version'
4
+ Gem::Specification.new do |s|
5
+ s.name = 'easy_form'
6
+ s.version = EasyForm::VERSION
7
+ s.date = '2015-05-26'
8
+ s.summary = "form builder without complex dsl"
9
+ s.description = "form builder without complex dsl"
10
+ s.authors = ["kikyous"]
11
+ s.email = 'kikyous@163.com'
12
+ s.files = `git ls-files`.split("\n")
13
+ s.homepage = 'https://github.com/kikyous/easy_form'
14
+ s.license = 'MIT'
15
+ end
@@ -0,0 +1,7 @@
1
+ module EasyForm
2
+ module Builder
3
+ def field(*args, &block)
4
+ EasyForm::Field.new(self, args, &block)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ module EasyForm
2
+ class Field
3
+ attr_accessor :builder, :type, :options, :args
4
+ def initialize(builder, args, &block)
5
+ @builder = builder
6
+ @options = args.extract_options!
7
+ @args = args
8
+ @block = block
9
+ end
10
+
11
+ def field
12
+ @args[1]
13
+ end
14
+
15
+ def model
16
+ builder.object.class
17
+ end
18
+
19
+ def input(options={})
20
+ builder.send(*args, @options.merge(options), &@block)
21
+ end
22
+
23
+ def required?
24
+ field && model.validators_on(field).any?{ActiveRecord::Validations::PresenceValidator}
25
+ end
26
+
27
+ def label_text
28
+ options[:label] || field.to_s.humanize
29
+ end
30
+
31
+ def to_s
32
+ EasyForm::Template.render(template_name, binding)
33
+ end
34
+
35
+ def template_name
36
+ builder.options[:template] || options[:template] || :default
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ module EasyForm
2
+ class Template
3
+ class << self
4
+ def store
5
+ return {} if Rails.env.development?
6
+ @store ||= {}
7
+ end
8
+
9
+ def cache(name)
10
+ store[name] ||= yield
11
+ end
12
+
13
+ def render(template_name, binding)
14
+ template = cache template_name do
15
+ File.read [Rails.root, '/app/views/easy_form/_', template_name, '.html.erb'].join
16
+ end
17
+ ERB.new(template).result(binding).html_safe
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module EasyForm
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,12 @@
1
+ module EasyForm
2
+ module Generators
3
+ class ViewGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../', __FILE__)
5
+ desc "This generator copy template files to app/views/easy_form"
6
+ def copy_default_template_file
7
+ copy_file "views/easy_form/_bootstrap.html.erb", 'app/views/easy_form/_bootstrap.html.erb'
8
+ copy_file "views/easy_form/_default.html.erb", 'app/views/easy_form/_default.html.erb'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ <div class="form-group">
2
+ <label for='<%= "#{builder.object_name}_#{field}" %>'>
3
+ <%= "#{'*' if required? } #{label_text}" %>
4
+ </label>
5
+ <%= input(class: 'form-control') %>
6
+ </div>
@@ -0,0 +1,7 @@
1
+ <div class='row'>
2
+ <div class='large-12 columns'>
3
+ <label><%= '*' if required? %> <%= label_text %>
4
+ <%= input %>
5
+ </label>
6
+ </div>
7
+ </div>
data/readme.markdown ADDED
@@ -0,0 +1,81 @@
1
+ EasyForm
2
+ ===
3
+ ### form builder without complex dsl
4
+
5
+ turn
6
+
7
+ ```ruby
8
+ <%= f.text_field, :title %>
9
+ ```
10
+ into
11
+
12
+ ```ruby
13
+ <%= f.field :text_field, :title %>
14
+ ```
15
+ > as you see `field` is a proxy method that send method and args to `ActionView::Helpers::FormBuilder` instance, so you can write like this: `
16
+ <%= f.field :text_area, :title %>
17
+ `
18
+
19
+ output:
20
+
21
+ ```html
22
+ <div class='row'>
23
+ <div class="large-12 columns">
24
+ <label>* Title
25
+ <input type="text" value="" name="article[title]" id="article_title">
26
+ </label>
27
+ </div>
28
+ </div>
29
+ ```
30
+ template above:
31
+
32
+ `app/views/easy_form/_default.html.erb`
33
+
34
+ ```erb
35
+ <div class='row'>
36
+ <div class='large-12 columns'>
37
+ <label><%= '*' if required? %> <%= label_text %>
38
+ <%= input %>
39
+ </label>
40
+ </div>
41
+ </div>
42
+
43
+ ```
44
+ or you can use a named template:
45
+
46
+ ```ruby
47
+ <%= f.field :text_field, :title, template: 'bootstrap' %>
48
+ ```
49
+ or globally:
50
+
51
+ ```ruby
52
+ <%= form_for @article, template: 'bootstrap' %>
53
+ ```
54
+ ###Install
55
+
56
+ `gem 'easy_form', github: 'kikyous/easy_form'`
57
+
58
+
59
+ ```
60
+ bundle
61
+ rails g easy_form:view
62
+ ```
63
+ ###More example
64
+ ```erb
65
+ <%= f.field :text_area, :content %>
66
+ <%= f.field :collection_check_boxes, :category_ids, Category.all, :id, :name %>
67
+ <%= f.field :collection_check_boxes, :category_ids, Category.all, :id, :name do |b| %>
68
+ <% b.label { b.check_box } %>
69
+ <% end %>
70
+
71
+ <%= f.field :collection_select, :user_id, User.all, :id, :name %>
72
+
73
+ ```
74
+
75
+ ###Method and object in template
76
+
77
+ - input (input tag generated with rails form builder)
78
+ - field (field, such as :title)
79
+ - label_text (text display as label)
80
+ - required? (this field is required?)
81
+ - builder (rails form builder)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kikyous
@@ -16,7 +16,16 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - easy_form.gemspec
19
20
  - lib/easy_form.rb
21
+ - lib/easy_form/builder.rb
22
+ - lib/easy_form/field.rb
23
+ - lib/easy_form/template.rb
24
+ - lib/easy_form/version.rb
25
+ - lib/generators/easy_form/view_generator.rb
26
+ - lib/generators/easy_form/views/easy_form/_bootstrap.html.erb
27
+ - lib/generators/easy_form/views/easy_form/_default.html.erb
28
+ - readme.markdown
20
29
  homepage: https://github.com/kikyous/easy_form
21
30
  licenses:
22
31
  - MIT