best_in_place_mongoid 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  require "best_in_place_mongoid/utils"
2
2
  require "best_in_place_mongoid/helper"
3
3
  require "best_in_place_mongoid/engine"
4
+ require "best_in_place_mongoid/railtie"
5
+ require "best_in_place_mongoid/display_methods"
4
6
 
5
7
  module BestInPlaceMongoid
6
8
  autoload :TestHelpers, "best_in_place_mongoid/test_helpers"
@@ -0,0 +1,38 @@
1
+ module BestInPlaceMongoid
2
+ module DisplayMethods
3
+ extend self
4
+
5
+ class Renderer < Struct.new(:opts)
6
+ def render_json(object)
7
+ case opts[:type]
8
+ when :model
9
+ {:display_as => object.send(opts[:method])}.to_json
10
+ when :helper
11
+ value = if opts[:helper_options]
12
+ BestInPlaceMongoid::ViewHelpers.send(opts[:method], object.send(opts[:attr]), opts[:helper_options])
13
+ else
14
+ BestInPlaceMongoid::ViewHelpers.send(opts[:method], object.send(opts[:attr]))
15
+ end
16
+ {:display_as => value}.to_json
17
+ else
18
+ {}.to_json
19
+ end
20
+ end
21
+ end
22
+
23
+ @@table = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
24
+
25
+ def lookup(klass, attr)
26
+ foo = @@table[klass.to_s][attr.to_s]
27
+ foo == {} ? nil : foo
28
+ end
29
+
30
+ def add_model_method(klass, attr, display_as)
31
+ @@table[klass.to_s][attr.to_s] = Renderer.new :method => display_as.to_sym, :type => :model
32
+ end
33
+
34
+ def add_helper_method(klass, attr, helper_method, helper_options = nil)
35
+ @@table[klass.to_s][attr.to_s] = Renderer.new :method => helper_method.to_sym, :type => :helper, :attr => attr, :helper_options => helper_options
36
+ end
37
+ end
38
+ end
@@ -2,10 +2,18 @@ module BestInPlaceMongoid
2
2
  module BestInPlaceMongoidHelpers
3
3
 
4
4
  def best_in_place(object, field, opts = {})
5
+ if opts[:display_as] && opts[:display_with]
6
+ raise ArgumentError, "Can't use both 'display_as' and 'display_with' options at the same time"
7
+ end
8
+
9
+ if opts[:display_with] && !opts[:display_with].is_a?(Proc) && !ViewHelpers.respond_to?(opts[:display_with])
10
+ raise ArgumentError, "Can't find helper #{opts[:display_with]}"
11
+ end
12
+
5
13
  opts[:type] ||= :input
6
14
  opts[:collection] ||= []
7
15
  field = field.to_s
8
- value = object.send(field).blank? ? "" : object.send(field)
16
+ value = build_value_for(object, field, opts)
9
17
  collection = nil
10
18
  if opts[:type] == :select && !opts[:collection].blank?
11
19
  v = object.send(field)
@@ -48,6 +56,33 @@ module BestInPlaceMongoid
48
56
  object.send field
49
57
  end
50
58
  end
59
+
60
+ private
61
+ def build_value_for(object, field, opts)
62
+ if opts[:display_as]
63
+ BestInPlaceMongoid::DisplayMethods.add_model_method(object.class.to_s, field, opts[:display_as])
64
+ object.send(opts[:display_as]).to_s
65
+
66
+ elsif opts[:display_with].try(:is_a?, Proc)
67
+ opts[:display_with].call(object.send(field))
68
+
69
+ elsif opts[:display_with]
70
+ BestInPlaceMongoid::DisplayMethods.add_helper_method(object.class.to_s, field, opts[:display_with], opts[:helper_options])
71
+ if opts[:helper_options]
72
+ BestInPlaceMongoid::ViewHelpers.send(opts[:display_with], object.send(field), opts[:helper_options])
73
+ else
74
+ BestInPlaceMongoid::ViewHelpers.send(opts[:display_with], object.send(field))
75
+ end
76
+
77
+ else
78
+ object.send(field).to_s.presence || ""
79
+ end
80
+ end
81
+
82
+ def attribute_escape(data)
83
+ data.to_s.gsub("&", "&amp;").gsub("'", "&apos;") unless data.nil?
84
+ end
85
+
51
86
  end
52
87
  end
53
88
 
@@ -0,0 +1,7 @@
1
+ module BestInPlaceMongoid
2
+ class Railtie < Rails::Railtie
3
+ initializer "set view helpers" do
4
+ BestInPlaceMongoid::ViewHelpers = ActionView::Base.new
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module BestInPlaceMongoid
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -19,6 +19,17 @@ describe BestInPlaceMongoid::BestInPlaceMongoidHelpers do
19
19
  span = nk.css("span")
20
20
  span.should_not be_empty
21
21
  end
22
+
23
+ describe "display as" do
24
+ before do
25
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name, display_as: :email)
26
+ @span = nk.css("span")
27
+ end
28
+
29
+ it "should have the email as the display" do
30
+ @span.text.should eq(@user.email)
31
+ end
32
+ end
22
33
 
23
34
  describe "general properties" do
24
35
  before do
metadata CHANGED
@@ -1,129 +1,94 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: best_in_place_mongoid
3
- version: !ruby/object:Gem::Version
4
- hash: 19
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 2
10
- version: 1.0.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Bernat Farrero
14
- - "Bart\xC5\x82omiej Danek"
9
+ - Bartłomiej Danek
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2011-11-15 00:00:00 +01:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
13
+ date: 2012-03-14 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
23
16
  name: rails
24
- prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &70126967129620 !ruby/object:Gem::Requirement
26
18
  none: false
27
- requirements:
19
+ requirements:
28
20
  - - ~>
29
- - !ruby/object:Gem::Version
30
- hash: 3
31
- segments:
32
- - 3
33
- - 1
34
- - 0
21
+ - !ruby/object:Gem::Version
35
22
  version: 3.1.0
36
23
  type: :runtime
37
- version_requirements: *id001
38
- - !ruby/object:Gem::Dependency
39
- name: mongoid
40
24
  prerelease: false
41
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *70126967129620
26
+ - !ruby/object:Gem::Dependency
27
+ name: mongoid
28
+ requirement: &70126967081460 !ruby/object:Gem::Requirement
42
29
  none: false
43
- requirements:
30
+ requirements:
44
31
  - - ~>
45
- - !ruby/object:Gem::Version
46
- hash: 5
47
- segments:
48
- - 2
49
- - 3
50
- - 3
32
+ - !ruby/object:Gem::Version
51
33
  version: 2.3.3
52
34
  type: :runtime
53
- version_requirements: *id002
54
- - !ruby/object:Gem::Dependency
55
- name: jquery-rails
56
35
  prerelease: false
57
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *70126967081460
37
+ - !ruby/object:Gem::Dependency
38
+ name: jquery-rails
39
+ requirement: &70126967068380 !ruby/object:Gem::Requirement
58
40
  none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- hash: 3
63
- segments:
64
- - 0
65
- version: "0"
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
66
45
  type: :runtime
67
- version_requirements: *id003
68
- - !ruby/object:Gem::Dependency
69
- name: rspec-rails
70
46
  prerelease: false
71
- requirement: &id004 !ruby/object:Gem::Requirement
47
+ version_requirements: *70126967068380
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec-rails
50
+ requirement: &70126967044320 !ruby/object:Gem::Requirement
72
51
  none: false
73
- requirements:
52
+ requirements:
74
53
  - - ~>
75
- - !ruby/object:Gem::Version
76
- hash: 19
77
- segments:
78
- - 2
79
- - 7
80
- - 0
54
+ - !ruby/object:Gem::Version
81
55
  version: 2.7.0
82
56
  type: :development
83
- version_requirements: *id004
84
- - !ruby/object:Gem::Dependency
85
- name: nokogiri
86
57
  prerelease: false
87
- requirement: &id005 !ruby/object:Gem::Requirement
58
+ version_requirements: *70126967044320
59
+ - !ruby/object:Gem::Dependency
60
+ name: nokogiri
61
+ requirement: &70126967011740 !ruby/object:Gem::Requirement
88
62
  none: false
89
- requirements:
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- hash: 3
93
- segments:
94
- - 1
95
- - 5
96
- - 0
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
97
66
  version: 1.5.0
98
67
  type: :development
99
- version_requirements: *id005
100
- - !ruby/object:Gem::Dependency
101
- name: capybara
102
68
  prerelease: false
103
- requirement: &id006 !ruby/object:Gem::Requirement
69
+ version_requirements: *70126967011740
70
+ - !ruby/object:Gem::Dependency
71
+ name: capybara
72
+ requirement: &70126966988980 !ruby/object:Gem::Requirement
104
73
  none: false
105
- requirements:
106
- - - ">="
107
- - !ruby/object:Gem::Version
108
- hash: 21
109
- segments:
110
- - 1
111
- - 0
112
- - 1
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
113
77
  version: 1.0.1
114
78
  type: :development
115
- version_requirements: *id006
116
- description: BestInPlaceMongoid is a fork of BestInPlace jQuery script and a Rails 3 helper that provide the method best_in_place to display any object field easily editable for the user by just clicking on it. It supports input data, text data, boolean data and custom dropdown data. It works with RESTful controllers.
117
- email:
79
+ prerelease: false
80
+ version_requirements: *70126966988980
81
+ description: BestInPlaceMongoid is a fork of BestInPlace jQuery script and a Rails
82
+ 3 helper that provide the method best_in_place to display any object field easily
83
+ editable for the user by just clicking on it. It supports input data, text data,
84
+ boolean data and custom dropdown data. It works with RESTful controllers.
85
+ email:
118
86
  - bernat@itnig.net
119
87
  - bartek.danek@gmail.com
120
88
  executables: []
121
-
122
89
  extensions: []
123
-
124
90
  extra_rdoc_files: []
125
-
126
- files:
91
+ files:
127
92
  - .gitignore
128
93
  - .rspec
129
94
  - Gemfile
@@ -133,8 +98,10 @@ files:
133
98
  - lib/assets/javascripts/best_in_place.js
134
99
  - lib/assets/javascripts/jquery.purr.js
135
100
  - lib/best_in_place_mongoid.rb
101
+ - lib/best_in_place_mongoid/display_methods.rb
136
102
  - lib/best_in_place_mongoid/engine.rb
137
103
  - lib/best_in_place_mongoid/helper.rb
104
+ - lib/best_in_place_mongoid/railtie.rb
138
105
  - lib/best_in_place_mongoid/test_helpers.rb
139
106
  - lib/best_in_place_mongoid/utils.rb
140
107
  - lib/best_in_place_mongoid/version.rb
@@ -143,39 +110,34 @@ files:
143
110
  - spec/integration/js_spec.rb
144
111
  - spec/integration/text_area_spec.rb
145
112
  - spec/spec_helper.rb
146
- has_rdoc: true
147
113
  homepage: http://github.com/bartekd/best_in_place_mongoid
148
114
  licenses: []
149
-
150
115
  post_install_message:
151
116
  rdoc_options: []
152
-
153
- require_paths:
117
+ require_paths:
154
118
  - lib
155
- required_ruby_version: !ruby/object:Gem::Requirement
119
+ required_ruby_version: !ruby/object:Gem::Requirement
156
120
  none: false
157
- requirements:
158
- - - ">="
159
- - !ruby/object:Gem::Version
160
- hash: 3
161
- segments:
162
- - 0
163
- version: "0"
164
- required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
126
  none: false
166
- requirements:
167
- - - ">="
168
- - !ruby/object:Gem::Version
169
- hash: 3
170
- segments:
171
- - 0
172
- version: "0"
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
173
131
  requirements: []
174
-
175
132
  rubyforge_project:
176
- rubygems_version: 1.5.3
133
+ rubygems_version: 1.8.17
177
134
  signing_key:
178
135
  specification_version: 3
179
- summary: It makes any field in place editable by clicking on it, it works for inputs, textareas, select dropdowns and checkboxes
180
- test_files: []
181
-
136
+ summary: It makes any field in place editable by clicking on it, it works for inputs,
137
+ textareas, select dropdowns and checkboxes
138
+ test_files:
139
+ - spec/helpers/best_in_place_mongoid_spec.rb
140
+ - spec/integration/double_init_spec.rb
141
+ - spec/integration/js_spec.rb
142
+ - spec/integration/text_area_spec.rb
143
+ - spec/spec_helper.rb