representations 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.
- data/README.markdown +7 -0
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/init.rb +1 -0
- data/lib/representations.rb +26 -16
- data/representations.gemspec +2 -2
- metadata +2 -2
data/README.markdown
CHANGED
@@ -42,3 +42,10 @@ Resource Representations
|
|
42
42
|
= p.last_name.text_field
|
43
43
|
= p.eye_color.radio_button('blue')
|
44
44
|
= p.eye_color.radio_button_label('blue', 'Blue')
|
45
|
+
###Extensions
|
46
|
+
Representations can be altered. For example to add new method DefaultRepresentation create file app/representations/default_representation.rb with the content:
|
47
|
+
module DefaultRepresentation
|
48
|
+
def new_method
|
49
|
+
some code
|
50
|
+
end
|
51
|
+
end
|
data/Rakefile
CHANGED
@@ -34,12 +34,12 @@ end
|
|
34
34
|
require 'spec/rake/spectask'
|
35
35
|
Spec::Rake::SpecTask.new(:spec) do |spec|
|
36
36
|
spec.libs << 'lib' << 'spec'
|
37
|
-
spec.spec_files = FileList['spec
|
37
|
+
spec.spec_files = FileList['spec/test_application/spec/helpers/*_spec.rb']
|
38
38
|
end
|
39
39
|
|
40
40
|
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
41
41
|
spec.libs << 'lib' << 'spec'
|
42
|
-
spec.pattern = 'spec
|
42
|
+
spec.pattern = 'spec/test_application/spec/helpers/*_spec.rb'
|
43
43
|
spec.rcov = true
|
44
44
|
end
|
45
45
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/init.rb
CHANGED
data/lib/representations.rb
CHANGED
@@ -16,13 +16,19 @@ module Representations
|
|
16
16
|
|
17
17
|
#value - object for which the representation is created
|
18
18
|
#template - template view
|
19
|
-
#name - the actuall name of the method that was called on the
|
19
|
+
#name - the actuall name of the method that was called on the object's parent that is being initialize
|
20
20
|
def initialize(value, template, name=nil, parent=nil)
|
21
21
|
@value = value
|
22
22
|
@name = name
|
23
23
|
@template = template
|
24
24
|
@parent = parent
|
25
|
-
|
25
|
+
begin #extend class if user provided file for customisation
|
26
|
+
self.send(:extend, "::#{self.class.to_s.demodulize}".constantize)
|
27
|
+
rescue
|
28
|
+
puts "::#{self.class.to_s.demodulize} not defined"
|
29
|
+
#procced then (user did not provide file for customisation of the class)
|
30
|
+
end
|
31
|
+
end
|
26
32
|
|
27
33
|
def id
|
28
34
|
@value
|
@@ -31,12 +37,8 @@ module Representations
|
|
31
37
|
def to_s
|
32
38
|
ERB::Util::h(@value.to_s)
|
33
39
|
end
|
34
|
-
#Call the passed block (if any)
|
35
|
-
def with_block(&block)
|
36
|
-
yield self if block_given?
|
37
|
-
end
|
38
40
|
#returns html label tag for the representation
|
39
|
-
def label(value, html_options = {})
|
41
|
+
def label(value = nil, html_options = {})
|
40
42
|
tree = get_parents_tree
|
41
43
|
for_attr_value = tree.join('_')
|
42
44
|
tags = get_tags(html_options, {:for => for_attr_value})
|
@@ -45,7 +47,11 @@ module Representations
|
|
45
47
|
end
|
46
48
|
|
47
49
|
protected
|
48
|
-
#
|
50
|
+
#Call the passed block (if any)
|
51
|
+
def with_block(&block)
|
52
|
+
yield self if block_given?
|
53
|
+
end
|
54
|
+
#Returns array of Represantation objects which are linked together by @parent field
|
49
55
|
def get_parents_tree
|
50
56
|
children_names = Array.new
|
51
57
|
parent = @parent
|
@@ -54,9 +60,9 @@ module Representations
|
|
54
60
|
children_names.push(parent.instance_variable_get(:@name))
|
55
61
|
parent = parent.instance_variable_get(:@parent)
|
56
62
|
end #children_names now looks something like that [name, profile, user]
|
57
|
-
children_names.reverse
|
63
|
+
children_names.reverse!
|
58
64
|
end
|
59
|
-
#
|
65
|
+
#Creates value of the html name attribute according to passed tree of objects
|
60
66
|
def get_html_name_attribute_value(tree)
|
61
67
|
root_name = tree.delete_at(0)
|
62
68
|
name = Array.new
|
@@ -69,16 +75,19 @@ module Representations
|
|
69
75
|
end
|
70
76
|
name.unshift(root_name)
|
71
77
|
end
|
72
|
-
#Returns string
|
78
|
+
#Returns string created by merging two hashes of html options passed as an argument
|
73
79
|
def get_tags(user_options, base_options)
|
74
|
-
base_options.merge
|
75
|
-
|
76
|
-
|
80
|
+
options = base_options.merge(user_options)
|
81
|
+
options.stringify_keys!
|
82
|
+
options = options.sort
|
83
|
+
options.map{ |key, value| %(#{key}="#{value}" ) }
|
77
84
|
end
|
78
85
|
end
|
79
86
|
|
80
87
|
class DefaultRepresentation < Representation
|
81
|
-
|
88
|
+
#require_dependency "#{RAILS_ROOT}/app/representations/default_representation"
|
89
|
+
#require "#{RAILS_ROOT}/app/representations/default_representation"
|
90
|
+
#include DefaultRepresentationExt
|
82
91
|
#not tested in the view
|
83
92
|
#Returns string with html check box tag
|
84
93
|
def check_box(checked_value = "1", unchecked_value = "0", html_options = {})
|
@@ -106,6 +115,7 @@ module Representations
|
|
106
115
|
end
|
107
116
|
#Returns string with html text input tag
|
108
117
|
def text_field(html_options = {})
|
118
|
+
puts "#{RAILS_ROOT}/app/representations/default_representation"
|
109
119
|
tree = get_parents_tree
|
110
120
|
id_attr_value = tree.join('_')
|
111
121
|
tags = get_tags(html_options, {:value => to_s, :id => id_attr_value, :name=>get_html_name_attribute_value(tree)})
|
@@ -174,7 +184,7 @@ module Representations
|
|
174
184
|
#ar_user = User.new
|
175
185
|
#ar_user.nick = 'foo'
|
176
186
|
#user = r(ar_user) #user is now ActiveRecordRepresentation
|
177
|
-
#user.nick.text_field #method_missing will be called on user with method_name = 'nick' in which new method for user will be created and will be called. The newly created method will create new DefaultRepresentation with @value set to the string 'foo'. Next the text_field will be called on the newly created DefauleRepresentation
|
187
|
+
#user.nick.text_field #method_missing will be called on user with method_name = 'nick' in which new method for user will be created and will be called. The newly created method will create a new DefaultRepresentation with @value set to the string 'foo'. Next the text_field will be called on the newly created DefauleRepresentation
|
178
188
|
#
|
179
189
|
def method_missing(method_name, *args, &block)
|
180
190
|
method = <<-EOF
|
data/representations.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{representations}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["\305\201ukasz Piestrzeniewicz", "Adam Sokolnicki"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-20}
|
13
13
|
s.description = %q{Rails helpers, including form builders are great to allow rapid development of applications and views.
|
14
14
|
They are procedural in nature and have hard time to adapt to complex models. They also live in a single namespace making it difficult to find which helpers apply to which models.
|
15
15
|
Resource representations change syntax to object oriented and model specific.}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: representations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "\xC5\x81ukasz Piestrzeniewicz"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-10-
|
13
|
+
date: 2009-10-20 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|