on_the_spot 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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile CHANGED
@@ -2,4 +2,9 @@ source "http://rubygems.org"
2
2
 
3
3
  gem "rake"
4
4
  gem "jeweler"
5
- gem "rspec", ">= 2.0.0.beta.20"
5
+ gem "json_pure"
6
+
7
+ group :test do
8
+ gem "rspec", ">= 2.0.0.rc"
9
+ gem "actionpack", ">=3.0.0"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionpack (3.0.0)
6
+ activemodel (= 3.0.0)
7
+ activesupport (= 3.0.0)
8
+ builder (~> 2.1.2)
9
+ erubis (~> 2.6.6)
10
+ i18n (~> 0.4.1)
11
+ rack (~> 1.2.1)
12
+ rack-mount (~> 0.6.12)
13
+ rack-test (~> 0.5.4)
14
+ tzinfo (~> 0.3.23)
15
+ activemodel (3.0.0)
16
+ activesupport (= 3.0.0)
17
+ builder (~> 2.1.2)
18
+ i18n (~> 0.4.1)
19
+ activesupport (3.0.0)
20
+ builder (2.1.2)
21
+ diff-lcs (1.1.2)
22
+ erubis (2.6.6)
23
+ abstract (>= 1.0.0)
24
+ gemcutter (0.6.1)
25
+ git (1.2.5)
26
+ i18n (0.4.1)
27
+ jeweler (1.4.0)
28
+ gemcutter (>= 0.1.0)
29
+ git (>= 1.2.5)
30
+ rubyforge (>= 2.0.0)
31
+ json_pure (1.4.6)
32
+ rack (1.2.1)
33
+ rack-mount (0.6.13)
34
+ rack (>= 1.0.0)
35
+ rack-test (0.5.6)
36
+ rack (>= 1.0)
37
+ rake (0.8.7)
38
+ rspec (2.0.0.rc)
39
+ rspec-core (= 2.0.0.rc)
40
+ rspec-expectations (= 2.0.0.rc)
41
+ rspec-mocks (= 2.0.0.rc)
42
+ rspec-core (2.0.0.rc)
43
+ rspec-expectations (2.0.0.rc)
44
+ diff-lcs (>= 1.1.2)
45
+ rspec-mocks (2.0.0.rc)
46
+ rspec-core (= 2.0.0.rc)
47
+ rspec-expectations (= 2.0.0.rc)
48
+ rubyforge (2.0.4)
49
+ json_pure (>= 1.1.7)
50
+ tzinfo (0.3.23)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ actionpack (>= 3.0.0)
57
+ jeweler
58
+ rake
59
+ rspec (>= 2.0.0.rc)
data/README.markdown CHANGED
@@ -1,7 +1,7 @@
1
1
  # on_the_spot
2
2
 
3
3
  On-the-spot is a Rails3 compliant unobtrusive javascript in-place-editing plugin, using jEditable.
4
- (currently a work in progress, extracted it from a rails3 application)
4
+ This is still in progress, but should already be usable for simple text-fields and text-areas.
5
5
 
6
6
  ## Installation
7
7
 
@@ -13,11 +13,7 @@ Run the installation task:
13
13
 
14
14
  rails g on_the_spot:install
15
15
 
16
- Inside your `routes.rb` you either provide the catch-all
17
-
18
- match ':controller(/:action(/:id(.:format)))'
19
-
20
- or you type something like
16
+ Inside your `routes.rb` you need to provide the following route:
21
17
 
22
18
  resources :posts do
23
19
  collection do
@@ -25,13 +21,17 @@ or you type something like
25
21
  end
26
22
  end
27
23
 
28
- While this last is the best solution, you need to do this for each controller that uses the on-the-spot editing.
24
+ You need to do this for each controller that uses the on-the-spot editing.
29
25
  For the moment i do not know of any better solution, but i am always open for suggestions!
30
26
 
31
27
  Inside your `application.html.haml` you will need to add below the default javascripts:
32
28
 
33
29
  = javascript_include_tag :on_the_spot
34
30
 
31
+ or using erb, you write
32
+
33
+ <%= javascript_include_tag :on_the_spot %>
34
+
35
35
  That is all you need to do to start using it!
36
36
 
37
37
 
@@ -58,21 +58,36 @@ It should be as simple as that :)
58
58
 
59
59
  The `on_the_spot_edit` also accepts options:
60
60
 
61
- * `type` : `textarea` or `select` (none means default edit, select is currently not yet supported)
61
+ * `type` : `textarea` or `select` (none means default edit)
62
62
  * `ok_text` : the text for the ok-button
63
63
  * `cancel_text` : the text for the cancel-button
64
64
  * `tooltip` : the tooltip-text
65
+ * `rows`: for textarea, the number of rows, defaults to 5
66
+ * `columns`: for textarea, the number of columns, defaults to 40
67
+ * `data`: for select, the lookup-data, should be in an array of id-value pairs. E.g. `[[1, 'ok'], [2, 'not ok'], [3, 'not decided']].
68
+
65
69
 
66
70
  For the texts: if a text is not specified, the default is taken from the `on_the_spot.en.yml` (or your current language).
67
71
 
68
- ## Example project
72
+ ## Example Usages
73
+
74
+ ### Edit field
69
75
 
70
- Ther is a example rails3-project called on_the_spot_tester here: http://github.com/nathanvda/on_the_spot_tester
76
+ <%= on_the_spot_edit @user, :name %>
77
+
78
+ ### Textarea
79
+
80
+ <%= on_the_spot_edit @user, :description, :type => :textarea, :rows => 10, :columns => 55 %>
81
+
82
+ ### Select-box
83
+
84
+ <%= on_the_spot_edit @user, :rating, :type => :select, :data => [[1, 'good'], [2, 'mediocre'], [3, 'bad']] %>
85
+
86
+
87
+ ## Example project
71
88
 
72
- ## To do
89
+ There is an example rails3-project called [on_the_spot_tester](http://github.com/nathanvda/on_the_spot_tester)
73
90
 
74
- - make sure user can choose to use a select instead of just text or textarea
75
- - add tests!
76
91
 
77
92
  ## Note on Patches/Pull Requests
78
93
 
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require "rspec/core/rake_task"
3
4
 
4
5
  begin
5
6
  require 'jeweler'
@@ -18,6 +19,14 @@ rescue LoadError
18
19
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
20
  end
20
21
 
22
+ RSpec::Core::RakeTask.new(:spec)
23
+
24
+ desc "Run all specs with rcov"
25
+ RSpec::Core::RakeTask.new("test_cov") do |t|
26
+ t.rcov = true
27
+ t.rcov_opts = %w{--rails --include views -Ispec --exclude gems\/,spec\/,features\/,seeds\/}
28
+ end
29
+
21
30
  require 'rake/rdoctask'
22
31
  Rake::RDocTask.new do |rdoc|
23
32
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
@@ -27,3 +36,4 @@ Rake::RDocTask.new do |rdoc|
27
36
  rdoc.rdoc_files.include('README*')
28
37
  rdoc.rdoc_files.include('lib/**/*.rb')
29
38
  end
39
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -6,18 +6,17 @@ $(document).ready(function() {
6
6
  $(".on_the_spot_editing").mouseout(function() {
7
7
  $(this).css('background-color', 'inherit');
8
8
  });
9
- /* $('.on_the_spot_editing').editable(this.attr('data-url'), {
10
- tooltip: 'Click to edit...',
11
- cancel : 'Cancel',
12
- submit : 'OK'
13
- });*/
14
9
  $('.on_the_spot_editing').each(function(n){
15
10
  var el = $(this),
16
11
  data_url = el.attr('data-url'),
17
12
  ok_text = el.attr('data-ok') || 'OK',
18
13
  cancel_text = el.attr('data-cancel') || 'Cancel',
19
14
  tooltip_text = el.attr('data-tooltip') || 'Click to edit ...',
20
- edit_type = el.attr('data-edittype');
15
+ edit_type = el.attr('data-edittype'),
16
+ select_data = el.attr('data-select'),
17
+ rows = el.attr('data-rows'),
18
+ columns = el.attr('data-columns'),
19
+ load_url = el.attr('data-loadurl');
21
20
 
22
21
  var options = {
23
22
  tooltip: tooltip_text,
@@ -27,6 +26,19 @@ $(document).ready(function() {
27
26
  if (edit_type != null) {
28
27
  options.type = edit_type;
29
28
  }
29
+ if (edit_type == 'select') {
30
+ if (select_data != null) {
31
+ options.data = select_data;
32
+ options.submitdata = { 'select_array': select_data }
33
+ }
34
+ if (load_url != null) {
35
+ options.loadurl = load_url;
36
+ }
37
+ }
38
+ else if (edit_type == 'textarea') {
39
+ options.rows = rows;
40
+ options.columns = columns;
41
+ }
30
42
 
31
43
  el.editable(data_url, options)
32
44
  })
@@ -11,9 +11,15 @@ module OnTheSpot
11
11
  def can_edit_on_the_spot
12
12
  define_method :update_attribute_on_the_spot do
13
13
  klass, field, id = params[:id].split('__')
14
+ select_data = params[:select_array]
14
15
  object = klass.camelize.constantize.find(id)
15
16
  object.update_attribute(field, params[:value])
16
- render :text => CGI::escapeHTML(object.send(field).to_s)
17
+ if select_data.nil?
18
+ render :text => CGI::escapeHTML(object.send(field).to_s)
19
+ else
20
+ parsed_data = JSON.parse(select_data.gsub("'", '"'))
21
+ render :text => parsed_data[object.send(field).to_s]
22
+ end
17
23
  end
18
24
  end
19
25
  end
@@ -4,11 +4,17 @@ module OnTheSpot
4
4
  EDIT_TYPE_TEXTAREA='textarea'
5
5
  EDIT_TYPE_SELECT='select'
6
6
 
7
+ class OnTheSpotMissingParameters < StandardError; end
8
+
7
9
  # Possible options:
8
10
  # ok_text : the ok-button text
9
11
  # cancel_text : the cancel-button text
10
12
  # tooltip : the tooltip to show
11
13
  # type : {'textarea' || 'select' }
14
+ # rows : nr of rows for textarea
15
+ # columns : nr of columns for textarea
16
+ # loadurl : (for select) an url that will return the json for the select
17
+ # data : (for select) an array of options in the form [id, value]
12
18
  def on_the_spot_edit(object, field, options={})
13
19
  #!!! to do: translate options to data-fields
14
20
  # Possible fields:
@@ -16,26 +22,65 @@ module OnTheSpot
16
22
  # type: textarea or not
17
23
  # button-translations ok-Text, cancel-Text
18
24
  #
25
+
26
+
27
+
19
28
  options.reverse_merge!(:ok_text => t('on_the_spot.ok'),
20
29
  :cancel_text => t('on_the_spot.cancel'),
21
- :tooltip => t('on_the_spot.tooltip')
30
+ :tooltip => t('on_the_spot.tooltip'),
31
+ :rows => 5,
32
+ :columns => 40
22
33
  )
23
34
 
24
35
  update_url = url_for(:action => 'update_attribute_on_the_spot')
25
36
 
37
+ field_value = object.send(field.to_sym).to_s
38
+
26
39
  html_options = { :id => "#{object.class.name.underscore}__#{field}__#{object.id}",
27
40
  :class => 'on_the_spot_editing',
28
41
  :'data-url' => update_url}
29
42
 
30
- html_options[:'data-edittype'] = options[:type] unless options[:type].nil?
31
- html_options[:'data-ok'] = options[:ok_text] unless options[:ok_text].nil?
32
- html_options[:'data-cancel'] = options[:cancel_text] unless options[:cancel_text].nil?
33
- html_options[:'data-tooltip'] = options[:tooltip] unless options[:tooltip].nil?
43
+ editable_type = options[:type].nil? ? nil : options[:type].to_sym
44
+ html_options[:'data-edittype'] = editable_type.to_s unless editable_type.nil?
45
+ if editable_type == :select
46
+ # we need either a data or loadurl
47
+ unless options[:loadurl].nil?
48
+ html_options[:'data-loadurl'] = options[:loadurl]
49
+ else
50
+ # we should find a hash
51
+ select_data = options[:data]
52
+ raise OnTheSpotMissingParameters.new("Using type select needs either data or loadurl to function!") if select_data.nil?
53
+ html_options[:'data-select'] = convert_array_to_json(select_data, field_value)
54
+ end
55
+ elsif editable_type == :textarea
56
+ html_options[:'data-rows'] = options[:rows]
57
+ html_options[:'data-columns'] = options[:columns]
58
+ end
59
+ html_options[:'data-ok'] = options[:ok_text]
60
+ html_options[:'data-cancel'] = options[:cancel_text]
61
+ html_options[:'data-tooltip'] = options[:tooltip]
34
62
 
35
63
  content_tag("span", html_options) do
36
- object.send(field.to_sym).to_s
64
+ if editable_type == :select
65
+ lookup_display_value(select_data, field_value)
66
+ else
67
+ field_value
68
+ end
37
69
  end
38
70
  end
39
71
 
72
+ def lookup_display_value(id_value_array, id_str)
73
+ found_pair = id_value_array.select{ |idv| idv[0].to_s == id_str.to_s }
74
+ found_pair.size == 0 ? '' : found_pair[0][1]
75
+ end
76
+
77
+ def convert_array_to_json(id_value_array, selected_id)
78
+ conv_arr = id_value_array.map{|idv| "'#{idv[0]}':'#{idv[1]}'" }
79
+ result = "{ #{conv_arr.join', '}"
80
+ result += ", 'selected':'#{ selected_id.to_s}'" unless selected_id.nil?
81
+ result += "}"
82
+ result
83
+ end
84
+
40
85
  end
41
86
  end
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.1"
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 = ["Nathan Van der Auwera"]
12
- s.date = %q{2010-09-11}
12
+ s.date = %q{2010-10-06}
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 = [
@@ -19,11 +19,14 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ ".rspec",
22
23
  "Gemfile",
24
+ "Gemfile.lock",
23
25
  "LICENSE",
24
26
  "README.markdown",
25
27
  "Rakefile",
26
28
  "VERSION",
29
+ "autotest/discover.rb",
27
30
  "lib/generators/on_the_spot/install/install_generator.rb",
28
31
  "lib/generators/on_the_spot/install/templates/jquery.jeditable.mini.js",
29
32
  "lib/generators/on_the_spot/install/templates/on_the_spot.en.yml",
@@ -1,7 +1,56 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'on_the_spot/on_the_spot_helpers'
3
+
4
+ require 'action_controller'
2
5
 
3
6
  describe "OnTheSpot" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
7
+ describe "Helpers" do
8
+ before(:each) do
9
+
10
+ class TestClass < ActionView::Base
11
+ include OnTheSpot::Helpers
12
+ end
13
+
14
+ @tester = TestClass.new
15
+ @test_array_nr = [[1,"abc"], [2, "def"], [3, "ghi"]]
16
+ @test_array_str = [["key", "value"], ["key2", "value2"]]
17
+ end
18
+
19
+ context "lookup values from array" do
20
+ it "should find value abc for key 1" do
21
+ @tester.lookup_display_value(@test_array_nr, 1).should == 'abc'
22
+ end
23
+
24
+ it "should find value <value2> for key key2" do
25
+ @tester.lookup_display_value(@test_array_str, 'key2').should == 'value2'
26
+ end
27
+
28
+ it "should return an empty string if key does not exist" do
29
+ @tester.lookup_display_value(@test_array_str, 'key1').should == ''
30
+ end
31
+
32
+ end
33
+
34
+ context "convert array to json" do
35
+ it "should convert correctly" do
36
+ @tester.convert_array_to_json(@test_array_nr, 1).should == "{ '1':'abc', '2':'def', '3':'ghi', 'selected':'1'}"
37
+ end
38
+ end
39
+
40
+ context "creating a simple edit-field" do
41
+ before(:each) do
42
+ @dummy = mock()
43
+ @dummy.stub!(:content).and_return('test')
44
+ @dummy.stub!(:id).and_return('123')
45
+ @tester.should_receive(:t).with('on_the_spot.ok').and_return("ok")
46
+ @tester.should_receive(:t).with('on_the_spot.cancel').and_return("cancel")
47
+ @tester.should_receive(:t).with('on_the_spot.tooltip').and_return("tooltip")
48
+ @tester.should_receive(:url_for).and_return('/bla')
49
+ @result = @tester.on_the_spot_edit @dummy, :content
50
+ end
51
+ it "should make the correct html" do
52
+ @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\">test</span>"
53
+ end
54
+ end
6
55
  end
7
56
  end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,26 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'on_the_spot'
4
- require 'spec'
5
- require 'spec/autorun'
3
+ require 'rspec/core'
4
+ #require 'autotest/rspec2'
6
5
 
7
- Spec::Runner.configure do |config|
8
-
6
+
7
+
8
+ RSpec.configure do |c|
9
+ c.color_enabled = true
10
+ # c.filter_run :focused => true
11
+ # c.run_all_when_everything_filtered = true
12
+ # c.filter_run_excluding :ruby => lambda {|version|
13
+ # case version.to_s
14
+ # when "!jruby"
15
+ # RUBY_ENGINE != "jruby"
16
+ # when /^> (.*)/
17
+ # !(RUBY_VERSION.to_s > $1)
18
+ # else
19
+ # !(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
20
+ # end
21
+ # }
22
+ # c.around do |example|
23
+ # sandboxed { example.run }
24
+ # end
9
25
  end
26
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nathan Van der Auwera
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-11 00:00:00 +02:00
17
+ date: 2010-10-06 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -45,11 +45,14 @@ extra_rdoc_files:
45
45
  files:
46
46
  - .document
47
47
  - .gitignore
48
+ - .rspec
48
49
  - Gemfile
50
+ - Gemfile.lock
49
51
  - LICENSE
50
52
  - README.markdown
51
53
  - Rakefile
52
54
  - VERSION
55
+ - autotest/discover.rb
53
56
  - lib/generators/on_the_spot/install/install_generator.rb
54
57
  - lib/generators/on_the_spot/install/templates/jquery.jeditable.mini.js
55
58
  - lib/generators/on_the_spot/install/templates/on_the_spot.en.yml