page_record 0.2.1 → 0.3.0
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.
- data/.gitmodules +3 -0
- data/Gemfile.lock +13 -5
- data/README.md +1 -1
- data/lib/page_record.rb +7 -0
- data/lib/page_record/form_builder.rb +23 -0
- data/lib/page_record/helpers.rb +175 -0
- data/lib/page_record/version.rb +1 -1
- metadata +11 -2
data/.gitmodules
ADDED
data/Gemfile.lock
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
page_record (0.
|
4
|
+
page_record (0.3.0)
|
5
5
|
activesupport
|
6
6
|
capybara (~> 2.1.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activesupport (
|
12
|
-
i18n (
|
13
|
-
|
11
|
+
activesupport (4.0.0)
|
12
|
+
i18n (~> 0.6, >= 0.6.4)
|
13
|
+
minitest (~> 4.2)
|
14
|
+
multi_json (~> 1.3)
|
15
|
+
thread_safe (~> 0.1)
|
16
|
+
tzinfo (~> 0.3.37)
|
17
|
+
atomic (1.1.10)
|
14
18
|
capybara (2.1.0)
|
15
19
|
mime-types (>= 1.16)
|
16
20
|
nokogiri (>= 1.3.3)
|
@@ -25,9 +29,10 @@ GEM
|
|
25
29
|
simplecov (>= 0.7)
|
26
30
|
thor
|
27
31
|
diff-lcs (1.2.4)
|
28
|
-
i18n (0.6.
|
32
|
+
i18n (0.6.4)
|
29
33
|
mime-types (1.23)
|
30
34
|
mini_portile (0.5.0)
|
35
|
+
minitest (4.7.5)
|
31
36
|
multi_json (1.7.7)
|
32
37
|
nokogiri (1.6.0)
|
33
38
|
mini_portile (~> 0.5.0)
|
@@ -56,7 +61,10 @@ GEM
|
|
56
61
|
rack-protection (~> 1.4)
|
57
62
|
tilt (~> 1.3, >= 1.3.4)
|
58
63
|
thor (0.18.1)
|
64
|
+
thread_safe (0.1.0)
|
65
|
+
atomic
|
59
66
|
tilt (1.4.1)
|
67
|
+
tzinfo (0.3.37)
|
60
68
|
xpath (2.0.0)
|
61
69
|
nokogiri (~> 1.3)
|
62
70
|
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://codeclimate.com/github/appdrones/page_record) [](https://travis-ci.org/appdrones/page_record) [](https://gemnasium.com/appdrones/page_record) [](https://coveralls.io/r/appdrones/page_record)
|
1
|
+
[](http://badge.fury.io/rb/page_record)[](https://codeclimate.com/github/appdrones/page_record) [](https://travis-ci.org/appdrones/page_record) [](https://gemnasium.com/appdrones/page_record) [](https://coveralls.io/r/appdrones/page_record)
|
2
2
|
|
3
3
|
# PageRecord
|
4
4
|
|
data/lib/page_record.rb
CHANGED
@@ -7,3 +7,10 @@ require "page_record/attribute_accessors"
|
|
7
7
|
require "page_record/class_actions"
|
8
8
|
require "page_record/class_methods"
|
9
9
|
require "page_record/errors"
|
10
|
+
begin
|
11
|
+
require "page_record/helpers"
|
12
|
+
require "page_record/form_builder"
|
13
|
+
rescue
|
14
|
+
# probably not in rails. Leave it like this
|
15
|
+
# TODO make this beter
|
16
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module PageRecord
|
2
|
+
|
3
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
4
|
+
include PageRecord::Helpers
|
5
|
+
|
6
|
+
helpers = field_helpers + %w(time_zone_select date_select) - %w(hidden_field fields_for label)
|
7
|
+
helpers.each do |helper|
|
8
|
+
define_method helper do |field, *args|
|
9
|
+
options = args.detect{ |a| a.is_a?(Hash) } || {}
|
10
|
+
super field, options.merge(attribute_for(field))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
actions = ['submit','button']
|
15
|
+
actions.each do |helper|
|
16
|
+
define_method helper do |field, *args|
|
17
|
+
options = args.detect{ |a| a.is_a?(Hash) } || {}
|
18
|
+
super field, options.merge(action_for(field.downcase))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
module PageRecord
|
2
|
+
|
3
|
+
module Helpers
|
4
|
+
|
5
|
+
##
|
6
|
+
#
|
7
|
+
# returns a hash containing the record-type and the id. The id is based on the type. For example when
|
8
|
+
# you specify `:team` as the type, it will search for the `@type` instance variable.
|
9
|
+
#
|
10
|
+
# example:
|
11
|
+
#
|
12
|
+
# ```ruby
|
13
|
+
# <%= form_for(@team, html:form_record_for(:team)) do |f| %>
|
14
|
+
# <% end %>
|
15
|
+
# ```
|
16
|
+
# this returns the follwing HTML:
|
17
|
+
#
|
18
|
+
# ```html
|
19
|
+
# <form accept-charset="UTF-8" action="/teams/2" class="edit_team" data-team-id="2" id="edit_team_2" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="patch"><input name="authenticity_token" type="hidden" value="QXIbPXH65Ek+8i0j2R8akdHX2WXLo2MuDFuUVL8CQpY="></div>
|
20
|
+
# </form>
|
21
|
+
# ```
|
22
|
+
#
|
23
|
+
# @param type Symbol identifying the type of record and the variable to use
|
24
|
+
# @param var the variable to use. This is optional
|
25
|
+
#
|
26
|
+
# @return Hash
|
27
|
+
#
|
28
|
+
#
|
29
|
+
def form_record_for(type, var=nil)
|
30
|
+
if var
|
31
|
+
id = var.id
|
32
|
+
else
|
33
|
+
id = instance_eval("@#{type}.id")
|
34
|
+
end
|
35
|
+
id ||= 'new'
|
36
|
+
@type = type
|
37
|
+
Hash["data-#{type}-id",id]
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
#
|
42
|
+
# Returns a hash containing the attribute name. This can be used as html options in rails helpers
|
43
|
+
#
|
44
|
+
# example in a form builder block:
|
45
|
+
#
|
46
|
+
# ```ruby
|
47
|
+
#<%= f.text_field :name, attribute_for(:name) %>
|
48
|
+
# ```
|
49
|
+
#
|
50
|
+
# this returns the follwing HTML:
|
51
|
+
#
|
52
|
+
# ```html
|
53
|
+
# <input data-attribute-for="name" id="team_name" name="team[name]" type="text">
|
54
|
+
# ```
|
55
|
+
#
|
56
|
+
# @param name Symbol or String identifying the name
|
57
|
+
#
|
58
|
+
# @return Hash
|
59
|
+
#
|
60
|
+
#
|
61
|
+
def attribute_for(name)
|
62
|
+
Hash["data-attribute-for",name]
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
#
|
67
|
+
# Returns a hash containing the action name. This can be used as html options in rails helpers
|
68
|
+
#
|
69
|
+
# example in a form builder block:
|
70
|
+
#
|
71
|
+
# ```ruby
|
72
|
+
# <%= f.submit "Submit", action_for(:save)%>
|
73
|
+
# ```
|
74
|
+
#
|
75
|
+
# this returns the follwing HTML:
|
76
|
+
#
|
77
|
+
# ```html
|
78
|
+
# <input data-action-for="submit" name="commit" type="submit" value="Submit">
|
79
|
+
# ```
|
80
|
+
#
|
81
|
+
# @param name Symbol or String identifying the action name
|
82
|
+
#
|
83
|
+
# @return Hash
|
84
|
+
#
|
85
|
+
#
|
86
|
+
def action_for(name)
|
87
|
+
Hash["data-action-for",name]
|
88
|
+
end
|
89
|
+
|
90
|
+
##
|
91
|
+
#
|
92
|
+
# Writes a tag containing the specified PageRecord attribute
|
93
|
+
#
|
94
|
+
# example:
|
95
|
+
#
|
96
|
+
# ```ruby
|
97
|
+
# <% @teams.each do |team| %>
|
98
|
+
# <tr>
|
99
|
+
# <%= attribute_tag_for(:td, :name) { team.name} %>
|
100
|
+
# <%= attribute_tag_for(:td, :competition) { team.competition} %>
|
101
|
+
# <%= attribute_tag_for(:td, :point) { team.point} %>
|
102
|
+
# <%= attribute_tag_for(:td, :ranking) { team.ranking} %>
|
103
|
+
# <td><%= link_to 'Show', team, action_for(:show) %></td>
|
104
|
+
# <td><%= link_to 'Edit', edit_team_path(team), action_for(:edit) %></td>
|
105
|
+
# <td><%= link_to 'Destroy', team, {method: :delete, data: { confirm: 'Are you sure?' }}.merge(action_for(:destroy)) %></td>
|
106
|
+
# </tr>
|
107
|
+
# ```
|
108
|
+
#
|
109
|
+
# the first `attribute_tag_for` lines returns the follwing HTML:
|
110
|
+
#
|
111
|
+
# ```html
|
112
|
+
#<td data-attribute-for="name">aa</td>
|
113
|
+
# ```
|
114
|
+
#
|
115
|
+
#
|
116
|
+
def attribute_tag_for(name, attribute, content_or_options_with_block = nil, options = nil, escape = true, &block)
|
117
|
+
options||= options ? options << {'data-attribute-for' => attribute} :{'data-attribute-for' => attribute}
|
118
|
+
if block_given?
|
119
|
+
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
|
120
|
+
content_tag_string(name, capture(&block), options, escape)
|
121
|
+
else
|
122
|
+
content_tag_string(name, content_or_options_with_block, options, escape)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
alias_method :atf, :attribute_tag_for
|
127
|
+
|
128
|
+
|
129
|
+
##
|
130
|
+
#
|
131
|
+
# build a form that automagicaly identifies a form as a PageRecord recognisable form
|
132
|
+
# The form is identified by the id of the given record or `new` if the record is new.
|
133
|
+
#
|
134
|
+
# All the elements in the form are labeled according to there field name. And thus easy
|
135
|
+
# recognisable by PageRecord
|
136
|
+
#
|
137
|
+
# example:
|
138
|
+
#
|
139
|
+
# ```ruby
|
140
|
+
# <%= record_form_for(@team) do |f| %>
|
141
|
+
# <div class="field" >
|
142
|
+
# <%= f.label :name %><br>
|
143
|
+
# <%= f.text_field :name %>
|
144
|
+
# </div>
|
145
|
+
# <div class="field"%>
|
146
|
+
# <%= f.label :competition %><br>
|
147
|
+
# <%= f.text_field :competition%>
|
148
|
+
# </div>
|
149
|
+
# <div class="actions">
|
150
|
+
# <%= f.submit "Submit"%>
|
151
|
+
# </div>
|
152
|
+
# <% end %>
|
153
|
+
# ```
|
154
|
+
# @see
|
155
|
+
#
|
156
|
+
# @return formBuilder object
|
157
|
+
#
|
158
|
+
#
|
159
|
+
def record_form_for(record, options = {}, &block)
|
160
|
+
case record
|
161
|
+
when String, Symbol
|
162
|
+
object_name = record
|
163
|
+
else
|
164
|
+
object = record.is_a?(Array) ? record.last : record
|
165
|
+
raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
|
166
|
+
object_name = options[:as] || model_name_from_record_or_class(object).param_key
|
167
|
+
end
|
168
|
+
options = options.merge(html:form_record_for(object_name), builder:PageRecord::FormBuilder)
|
169
|
+
form_for(record, options, &block)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
ActionView::Base.send :include, PageRecord::Helpers
|
data/lib/page_record/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: page_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -116,6 +116,7 @@ extra_rdoc_files: []
|
|
116
116
|
files:
|
117
117
|
- .coveralls.yml
|
118
118
|
- .gitignore
|
119
|
+
- .gitmodules
|
119
120
|
- .rspec
|
120
121
|
- .travis.yml
|
121
122
|
- .yardopts
|
@@ -133,6 +134,8 @@ files:
|
|
133
134
|
- lib/page_record/cucumber.rb
|
134
135
|
- lib/page_record/errors.rb
|
135
136
|
- lib/page_record/finders.rb
|
137
|
+
- lib/page_record/form_builder.rb
|
138
|
+
- lib/page_record/helpers.rb
|
136
139
|
- lib/page_record/instance_actions.rb
|
137
140
|
- lib/page_record/rspec.rb
|
138
141
|
- lib/page_record/version.rb
|
@@ -162,12 +165,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
162
165
|
- - ! '>='
|
163
166
|
- !ruby/object:Gem::Version
|
164
167
|
version: '0'
|
168
|
+
segments:
|
169
|
+
- 0
|
170
|
+
hash: 3410867289688860735
|
165
171
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
172
|
none: false
|
167
173
|
requirements:
|
168
174
|
- - ! '>='
|
169
175
|
- !ruby/object:Gem::Version
|
170
176
|
version: '0'
|
177
|
+
segments:
|
178
|
+
- 0
|
179
|
+
hash: 3410867289688860735
|
171
180
|
requirements: []
|
172
181
|
rubyforge_project:
|
173
182
|
rubygems_version: 1.8.24
|