on_the_spot 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Change History / Release Notes
2
2
 
3
+ ## Version 0.0.18 (11/12/2011)
4
+
5
+ * added support for a `:display_method` which will make working with formatted editable content much easier
6
+ * added the option to specify a function to verify the access
7
+
3
8
  ## Version 0.0.17 (1/11/2011)
4
9
 
5
10
  * added support for `onblur` action: when focus is lost, allow to autosave.
data/README.markdown CHANGED
@@ -4,6 +4,17 @@
4
4
 
5
5
  On-the-spot is a Rails3 compliant unobtrusive javascript in-place-editing plugin, using jEditable, and depends on jQuery.
6
6
 
7
+ ## Features
8
+
9
+ * built on proven jQuery plugin `jEditable`
10
+ * works on index-pages, nested objects, ...
11
+ * can generate simple edit-boxes, textareas, dropdown lists, checkboxes
12
+ * will check your server-side validations and show the error
13
+ * you can check the access-rights before doing any update (to check against tampering)
14
+ * you can use custom display methods, e.g when using markdown
15
+ * watch the demo-project [here]() ([source](https://github.com/nathanvda/on_the_spot_tester))
16
+
17
+
7
18
  ## Installation
8
19
 
9
20
  Inside your `Gemfile` add the following:
@@ -26,6 +37,14 @@ Or, inside your `application.html.haml` you could still include the needed javas
26
37
 
27
38
  = javascript_include_tag :on_the_spot
28
39
 
40
+ To use the default styling, add the following to application.css so it compiles to the asset_pipeline
41
+
42
+ //= require on_the_spot
43
+
44
+ Or, inside your `application.html.haml` you could still include the needed css, using
45
+
46
+ = stylesheet_link_tag :on_the_spot
47
+
29
48
 
30
49
  ### Rails 3.0.x
31
50
 
@@ -37,6 +56,15 @@ or using erb, you write
37
56
 
38
57
  <%= javascript_include_tag :on_the_spot %>
39
58
 
59
+ To use the default styling, inside your `application.html.haml` you will need to add below the default CSS:
60
+
61
+ = stylesheet_link_tag :on_the_spot
62
+
63
+ or using erb, you write
64
+
65
+ <%= stylesheet_link_tag :on_the_spot %>
66
+
67
+
40
68
  ### Routes (for all Rails 3 versions)
41
69
 
42
70
  Inside your `routes.rb` you need to provide the following route :
@@ -44,12 +72,14 @@ Inside your `routes.rb` you need to provide the following route :
44
72
  resources :posts do
45
73
  collection do
46
74
  put :update_attribute_on_the_spot
75
+ get :get_attribute_on_the_spot
47
76
  end
48
77
  end
49
78
 
50
79
  You need to do this for each controller that uses the on-the-spot editing.
51
- For the moment i do not know of any better solution, but i am always open for suggestions!
52
80
 
81
+ You only need to specify the route for `get_attribute_on_the_spot` if you make use of the `:display_method` option,
82
+ and do not want to supply your own load-function.
53
83
 
54
84
  That is all you need to do to start using it!
55
85
 
@@ -89,10 +119,21 @@ The `on_the_spot_edit` also accepts options:
89
119
  * `:url`: URL to post to if you don't want to use the standard routes
90
120
  * `:selected`: Text selected by default on edit (boolean, default is false)
91
121
  * `:callback`: The name of a javascript function that is called after form has been submitted
122
+ * `:display_method`: the name of a method that is used to get the value to display of a field. When you use this, we will automatically
123
+ attempt to look up the raw value of the field to edit. This differs from the `:display_text` option, as this will also be called after update.
124
+ This supersedes the `:display_text` option.
92
125
 
93
126
 
94
127
  For the texts: if a text is not specified, the default is taken from the `on_the_spot.en.yml` (or your current language).
95
128
 
129
+ ## Styling
130
+
131
+ Each element that is editable will have the `on_the_spot_editing` class.
132
+
133
+ When an element is moused over, it will get the `on_the_spot_over` class.
134
+
135
+ You can use these classes to style the elements.
136
+
96
137
  ## Example Usages
97
138
 
98
139
  ### Edit field
@@ -119,6 +160,31 @@ For the texts: if a text is not specified, the default is taken from the `on_the
119
160
  <%= on_the_spot_edit @user, :name, :callback => 'testCallback' %>
120
161
 
121
162
 
163
+ ## Using together with `cancan`
164
+
165
+ When using `on_the_spot` together with `cancan`, you will have to explicitly exclude the on_the_spot method,
166
+ like so:
167
+
168
+ before_filter :load_and_authorize_resource, :except => [:update_attribute_on_the_spot]
169
+
170
+ The `load_and_authorize_resource` will try to find the object, based on the id in the parameters, but `on_the_spot` uses a different
171
+ encoding to store the object, field and id in one attribute. So if you exclude that, there will not be a problem.
172
+
173
+
174
+ ## Using together with an authorization system (e.g. Cancan)
175
+
176
+ If you want to test access-rights, you can do so by specifying a method which will be called
177
+
178
+ In your controller write:
179
+
180
+ can_edit_on_the_spot :check_access
181
+
182
+ def check_access(object, field)
183
+ # verify that the current user has access to edit/see the field of given object
184
+ end
185
+
186
+
187
+
122
188
  ## Example project
123
189
 
124
190
  There is an example rails3-project called [on_the_spot_tester](http://github.com/nathanvda/on_the_spot_tester)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.17
1
+ 0.0.18
@@ -0,0 +1 @@
1
+ .on_the_spot_over { background: #EEF2A0; }
@@ -1,10 +1,10 @@
1
1
  $(document).ready(function() {
2
2
 
3
3
  $(".on_the_spot_editing").mouseover(function() {
4
- $(this).css('background-color', '#EEF2A0');
4
+ $(this).addClass('on_the_spot_over');
5
5
  });
6
6
  $(".on_the_spot_editing").mouseout(function() {
7
- $(this).css('background-color', 'inherit');
7
+ $(this).removeClass('on_the_spot_over');
8
8
  });
9
9
  $('.on_the_spot_editing').each(initializeOnTheSpot);
10
10
 
@@ -24,6 +24,7 @@ function initializeOnTheSpot(n){
24
24
  load_url = el.attr('data-loadurl'),
25
25
  selected = el.attr('data-selected'),
26
26
  onblur_action = el.attr('data-onblur') || 'cancel',
27
+ method_name = el.attr('data-display-method') || '',
27
28
  callback = el.attr('data-callback');
28
29
 
29
30
 
@@ -42,6 +43,7 @@ function initializeOnTheSpot(n){
42
43
  onblur: onblur_action,
43
44
  submitdata: {
44
45
  authenticity_token: auth_token,
46
+ display_method: method_name,
45
47
  _method: 'put'
46
48
  },
47
49
  callback: callback ? new Function("value", "settings", "return "+callback+"(this, value, settings);") : null
@@ -63,5 +65,5 @@ function initializeOnTheSpot(n){
63
65
  options.cols = columns;
64
66
  }
65
67
 
66
- el.editable(data_url, options)
68
+ el.editable(data_url, options);
67
69
  }
@@ -13,7 +13,7 @@ module OnTheSpot
13
13
  if ::Rails.version[0..2].to_f >= 3.1
14
14
  #puts "The javascripts do not need to be installed since Rails 3.1"
15
15
  else
16
- copy_file "../../../../../app/assets/javascripts/on_the_spot.js", "public/javascripts/on_the_spot.js"
16
+ copy_file "../../../../../app/assets/javascripts/on_the_spot_code.js", "public/javascripts/on_the_spot.js"
17
17
  copy_file "../../../../../app/assets/javascripts/jquery.jeditable.mini.js", "public/javascripts/jquery.jeditable.mini.js"
18
18
  copy_file "../../../../../app/assets/javascripts/jquery.jeditable.checkbox.js", "public/javascripts/jquery.jeditable.checkbox.js"
19
19
  end
@@ -8,20 +8,48 @@ module OnTheSpot
8
8
  # if this method is called inside a controller, the edit-on-the-spot
9
9
  # controller action is added that will allow to edit fields in place
10
10
  module ClassMethods
11
- def can_edit_on_the_spot
11
+ def can_edit_on_the_spot(check_acces_method=nil)
12
12
  define_method :update_attribute_on_the_spot do
13
13
  klass, field, id = params[:id].split('__')
14
14
  select_data = params[:select_array]
15
+ display_method = params[:display_method]
16
+
15
17
  object = klass.camelize.constantize.find(id)
16
- if object.update_attributes(field => params[:value])
17
- if select_data.nil?
18
- render :text => CGI::escapeHTML(object.send(field).to_s)
18
+
19
+ is_allowed = check_acces_method.present? ? self.send(check_acces_method, object, field) : true
20
+
21
+ if is_allowed
22
+ if object.update_attributes(field => params[:value])
23
+ if select_data.nil?
24
+ field_or_method = if display_method.present?
25
+ object.send(display_method)
26
+ else
27
+ CGI::escapeHTML(object.send(field).to_s)
28
+ end
29
+ render :text => field_or_method
30
+ else
31
+ parsed_data = JSON.parse(select_data.gsub("'", '"'))
32
+ render :text => parsed_data[object.send(field).to_s]
33
+ end
19
34
  else
20
- parsed_data = JSON.parse(select_data.gsub("'", '"'))
21
- render :text => parsed_data[object.send(field).to_s]
35
+ render :text => object.errors.full_messages.join("\n"), :status => 422
22
36
  end
23
37
  else
24
- render :text => object.errors.full_messages.join("\n"), :status => 422
38
+ render :text => "Acces is not allowed", :status => 422
39
+ end
40
+ end
41
+
42
+ define_method :get_attribute_on_the_spot do
43
+ klass, field, id = params[:id].split('__')
44
+
45
+ object = klass.camelize.constantize.find(id)
46
+
47
+ is_allowed = check_acces_method.present? ? self.send(check_acces_method, object, field) : true
48
+
49
+ if is_allowed
50
+ render :text => object.send(field)
51
+ else
52
+ render :text => "Acces is not allowed", :status => 422
25
53
  end
26
54
  end
27
55
  end
@@ -20,14 +20,6 @@ module OnTheSpot
20
20
  # selected : (optional) boolean, text selected on edit
21
21
  # callback : (optional) a javascript function that is called after form has been submitted
22
22
  def on_the_spot_edit(object, field, options={})
23
- #!!! to do: translate options to data-fields
24
- # Possible fields:
25
- # type: textarea or not
26
- # button-translations ok-Text, cancel-Text
27
- #
28
-
29
-
30
-
31
23
  options.reverse_merge!(:ok_text => t('on_the_spot.ok'),
32
24
  :cancel_text => t('on_the_spot.cancel'),
33
25
  :tooltip => t('on_the_spot.tooltip'),
@@ -52,21 +44,27 @@ module OnTheSpot
52
44
  raise OnTheSpotMissingParameters.new("Using type select needs either data or loadurl to function!") if select_data.nil?
53
45
  html_options[:'data-select'] = convert_array_to_json(select_data, field_value)
54
46
  elsif editable_type == :textarea
55
- html_options[:'data-rows'] = options[:rows]
56
- html_options[:'data-columns'] = options[:columns]
47
+ html_options[:'data-rows'] = options[:rows]
48
+ html_options[:'data-columns'] = options[:columns]
49
+ end
50
+ html_options[:'data-ok'] = options[:ok_text]
51
+ html_options[:'data-cancel'] = options[:cancel_text]
52
+ html_options[:'data-tooltip'] = options[:tooltip]
53
+ html_options[:'data-auth'] = form_authenticity_token if defined? form_authenticity_token
54
+ html_options[:'data-selected'] = options[:selected]
55
+ html_options[:'data-callback'] = options[:callback]
56
+ html_options[:'data-onblur'] = options[:onblur] if options[:onblur] && ['cancel','submit', 'ignore'].include?(options[:onblur])
57
+ html_options[:'data-loadurl'] = options[:loadurl] unless options[:loadurl].nil?
58
+ html_options[:'data-display-method'] = options[:display_method] unless options[:display_method].nil?
59
+ if html_options[:'data-display-method'].present? && html_options[:'data-loadurl'].nil?
60
+ html_options[:'data-loadurl'] = url_for(:action => 'get_attribute_on_the_spot')
57
61
  end
58
- html_options[:'data-ok'] = options[:ok_text]
59
- html_options[:'data-cancel'] = options[:cancel_text]
60
- html_options[:'data-tooltip'] = options[:tooltip]
61
- html_options[:'data-auth'] = form_authenticity_token if defined? form_authenticity_token
62
- html_options[:'data-selected'] = options[:selected]
63
- html_options[:'data-callback'] = options[:callback]
64
- html_options[:'data-onblur'] = options[:onblur] if options[:onblur] && ['cancel','submit', 'ignore'].include?(options[:onblur])
65
- html_options[:'data-loadurl'] = options[:loadurl] unless options[:loadurl].nil?
66
62
 
67
63
  content_tag("span", html_options) do
68
64
  if options[:display_text]
69
65
  options[:display_text]
66
+ elsif options[:display_method]
67
+ object.send(options[:display_method].to_sym).to_s
70
68
  elsif editable_type == :select && options[:loadurl].nil?
71
69
  lookup_display_value(select_data, field_value)
72
70
  else
data/on_the_spot.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{on_the_spot}
8
- s.version = "0.0.17"
8
+ s.version = "0.0.18"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Van der Auwera"]
12
- s.date = %q{2011-11-06}
12
+ s.date = %q{2011-12-12}
13
13
  s.description = %q{Unobtrusive in place editing, using jEditable; only works in Rails 3}
14
14
  s.email = %q{nathan@dixis.com}
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "README.markdown",
29
29
  "Rakefile",
30
30
  "VERSION",
31
+ "app/assets/css/on_the_spot.css",
31
32
  "app/assets/javascripts/jquery.jeditable.checkbox.js",
32
33
  "app/assets/javascripts/jquery.jeditable.mini.js",
33
34
  "app/assets/javascripts/on_the_spot.js",
@@ -67,6 +67,23 @@ describe "OnTheSpot" do
67
67
  @result.should == "<span class=\"on_the_spot_editing\" data-cancel=\"cancel\" data-ok=\"ok\" data-tooltip=\"tooltip\" data-url=\"/bla\" id=\"r_spec/mocks/mock__content__123\">jediknight</span>"
68
68
  end
69
69
 
70
+ it "makes the correct html for an edit-field and use the display-method as string" do
71
+ @dummy.should_receive(:changed_content).and_return("test-changed")
72
+ @tester.should_receive(:url_for).with({:action => 'get_attribute_on_the_spot'}).and_return('/bla-again')
73
+
74
+ @result = @tester.on_the_spot_edit @dummy, :content, :display_method => 'changed_content'
75
+ @result.should == "<span class=\"on_the_spot_editing\" data-cancel=\"cancel\" data-display-method=\"changed_content\" data-loadurl=\"/bla-again\" data-ok=\"ok\" data-tooltip=\"tooltip\" data-url=\"/bla\" id=\"r_spec/mocks/mock__content__123\">test-changed</span>"
76
+ end
77
+
78
+ it "makes the correct html for an edit-field and use the display-method (as symbol)" do
79
+ @dummy.should_receive(:changed_content).and_return("test-changed")
80
+ @tester.should_receive(:url_for).with({:action => 'get_attribute_on_the_spot'}).and_return('/bla-again')
81
+
82
+ @result = @tester.on_the_spot_edit @dummy, :content, :display_method => :changed_content
83
+ @result.should == "<span class=\"on_the_spot_editing\" data-cancel=\"cancel\" data-display-method=\"changed_content\" data-loadurl=\"/bla-again\" data-ok=\"ok\" data-tooltip=\"tooltip\" data-url=\"/bla\" id=\"r_spec/mocks/mock__content__123\">test-changed</span>"
84
+ end
85
+
86
+
70
87
  it "makes the correct html for a text-area" do
71
88
  @result = @tester.on_the_spot_edit @dummy, :content, :type => :textarea
72
89
  @result.should == "<span class=\"on_the_spot_editing\" data-cancel=\"cancel\" data-columns=\"40\" data-edittype=\"textarea\" data-ok=\"ok\" data-rows=\"5\" data-tooltip=\"tooltip\" data-url=\"/bla\" id=\"r_spec/mocks/mock__content__123\">test</span>"
@@ -109,7 +126,7 @@ describe "OnTheSpot" do
109
126
  @result.should == "<span class=\"on_the_spot_editing\" data-cancel=\"cancel\" data-edittype=\"select\" data-loadurl=\"/load/data\" data-ok=\"ok\" data-tooltip=\"tooltip\" data-url=\"/bla\" id=\"r_spec/mocks/mock__content__123\">test</span>"
110
127
  end
111
128
 
112
- it "usea the display-text preferrably" do
129
+ it "use the display-text preferrably" do
113
130
  @result = @tester.on_the_spot_edit @dummy, :content, :type => :select, :loadurl => '/load/data', :display_text => 'ninja'
114
131
  @result.should == "<span class=\"on_the_spot_editing\" data-cancel=\"cancel\" data-edittype=\"select\" data-loadurl=\"/load/data\" data-ok=\"ok\" data-tooltip=\"tooltip\" data-url=\"/bla\" id=\"r_spec/mocks/mock__content__123\">ninja</span>"
115
132
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: on_the_spot
3
3
  version: !ruby/object:Gem::Version
4
- hash: 61
4
+ hash: 59
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 17
10
- version: 0.0.17
9
+ - 18
10
+ version: 0.0.18
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nathan Van der Auwera
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-06 00:00:00 +01:00
18
+ date: 2011-12-12 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -88,6 +88,7 @@ files:
88
88
  - README.markdown
89
89
  - Rakefile
90
90
  - VERSION
91
+ - app/assets/css/on_the_spot.css
91
92
  - app/assets/javascripts/jquery.jeditable.checkbox.js
92
93
  - app/assets/javascripts/jquery.jeditable.mini.js
93
94
  - app/assets/javascripts/on_the_spot.js