best_in_place 2.1.0 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -5
  3. data/.rspec +1 -0
  4. data/.travis.yml +12 -5
  5. data/Appraisals +17 -0
  6. data/CHANGELOG.md +51 -30
  7. data/Gemfile +15 -2
  8. data/README.md +52 -105
  9. data/best_in_place.gemspec +13 -11
  10. data/config.ru +7 -0
  11. data/gemfiles/rails_3.2.gemfile +24 -0
  12. data/gemfiles/rails_4.0.gemfile +23 -0
  13. data/gemfiles/rails_4.1.gemfile +23 -0
  14. data/gemfiles/rails_edge.gemfile +25 -0
  15. data/lib/assets/javascripts/best_in_place.jquery-ui.js +57 -0
  16. data/lib/assets/javascripts/best_in_place.js +551 -650
  17. data/lib/assets/javascripts/best_in_place.purr.js +16 -6
  18. data/lib/best_in_place.rb +29 -9
  19. data/lib/best_in_place/controller_extensions.rb +10 -13
  20. data/lib/best_in_place/display_methods.rb +26 -21
  21. data/lib/best_in_place/engine.rb +2 -2
  22. data/lib/best_in_place/helper.rb +145 -87
  23. data/lib/best_in_place/railtie.rb +5 -2
  24. data/lib/best_in_place/test_helpers.rb +0 -1
  25. data/lib/best_in_place/utils.rb +20 -12
  26. data/lib/best_in_place/version.rb +1 -1
  27. data/spec/{helpers/best_in_place_spec.rb → helper_spec.rb} +134 -99
  28. data/spec/integration/double_init_spec.rb +3 -5
  29. data/spec/integration/js_spec.rb +193 -123
  30. data/spec/integration/live_spec.rb +3 -4
  31. data/spec/integration/text_area_spec.rb +4 -4
  32. data/spec/internal/app/assets/images/info.png +0 -0
  33. data/{test_app → spec/internal}/app/assets/images/no.png +0 -0
  34. data/spec/internal/app/assets/images/purrBottom.png +0 -0
  35. data/spec/internal/app/assets/images/purrClose.png +0 -0
  36. data/spec/internal/app/assets/images/purrTop.png +0 -0
  37. data/{test_app → spec/internal}/app/assets/images/red_pen.png +0 -0
  38. data/{test_app → spec/internal}/app/assets/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  39. data/{test_app → spec/internal}/app/assets/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  40. data/{test_app → spec/internal}/app/assets/images/ui-bg_flat_10_000000_40x100.png +0 -0
  41. data/{test_app → spec/internal}/app/assets/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  42. data/{test_app → spec/internal}/app/assets/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  43. data/{test_app → spec/internal}/app/assets/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  44. data/{test_app → spec/internal}/app/assets/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  45. data/{test_app → spec/internal}/app/assets/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  46. data/{test_app → spec/internal}/app/assets/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  47. data/{test_app → spec/internal}/app/assets/images/ui-icons_222222_256x240.png +0 -0
  48. data/{test_app → spec/internal}/app/assets/images/ui-icons_228ef1_256x240.png +0 -0
  49. data/{test_app → spec/internal}/app/assets/images/ui-icons_ef8c08_256x240.png +0 -0
  50. data/{test_app → spec/internal}/app/assets/images/ui-icons_ffd27a_256x240.png +0 -0
  51. data/{test_app → spec/internal}/app/assets/images/ui-icons_ffffff_256x240.png +0 -0
  52. data/{test_app → spec/internal}/app/assets/images/yes.png +0 -0
  53. data/spec/internal/app/assets/javascripts/application.js +37 -0
  54. data/{test_app → spec/internal}/app/assets/stylesheets/.gitkeep +0 -0
  55. data/{test_app → spec/internal}/app/assets/stylesheets/jquery-ui-1.8.16.custom.css.erb +1 -2
  56. data/{test_app → spec/internal}/app/assets/stylesheets/scaffold.css +1 -1
  57. data/{test_app → spec/internal}/app/assets/stylesheets/style.css.erb +2 -4
  58. data/{test_app → spec/internal}/app/controllers/admin/users_controller.rb +8 -3
  59. data/{test_app → spec/internal}/app/controllers/application_controller.rb +0 -0
  60. data/{test_app → spec/internal}/app/controllers/cuca/cars_controller.rb +0 -0
  61. data/{test_app → spec/internal}/app/controllers/users_controller.rb +8 -40
  62. data/{test_app → spec/internal}/app/helpers/application_helper.rb +0 -0
  63. data/spec/internal/app/helpers/users_helper.rb +29 -0
  64. data/{test_app → spec/internal}/app/models/cuca/car.rb +0 -0
  65. data/{test_app → spec/internal}/app/models/user.rb +5 -1
  66. data/{test_app → spec/internal}/app/views/admin/users/show.html.erb +2 -2
  67. data/{test_app → spec/internal}/app/views/cuca/cars/show.html.erb +0 -0
  68. data/{test_app → spec/internal}/app/views/layouts/application.html.erb +1 -1
  69. data/{test_app → spec/internal}/app/views/users/_form.html.erb +2 -2
  70. data/{test_app → spec/internal}/app/views/users/double_init.html.erb +4 -10
  71. data/spec/internal/app/views/users/edit.html.erb +5 -0
  72. data/{test_app → spec/internal}/app/views/users/email_field.html.erb +0 -0
  73. data/{test_app → spec/internal}/app/views/users/index.html.erb +0 -0
  74. data/{test_app → spec/internal}/app/views/users/new.html.erb +0 -0
  75. data/{test_app → spec/internal}/app/views/users/show.html.erb +32 -24
  76. data/{test_app → spec/internal}/app/views/users/show_ajax.html.erb +0 -0
  77. data/spec/internal/config/database.yml +5 -0
  78. data/{test_app → spec/internal}/config/initializers/countries.rb +0 -0
  79. data/{test_app → spec/internal}/config/initializers/default_date_format.rb +0 -0
  80. data/spec/internal/config/initializers/development.rb +8 -0
  81. data/{test_app → spec/internal}/config/routes.rb +1 -2
  82. data/spec/internal/db/schema.rb +26 -0
  83. data/{test_app → spec/internal}/public/favicon.ico +0 -0
  84. data/spec/rails_helper.rb +21 -0
  85. data/spec/support/retry_on_timeout.rb +4 -7
  86. data/spec/utils_spec.rb +21 -0
  87. data/vendor/assets/javascripts/jquery.autosize.js +272 -0
  88. data/{lib → vendor}/assets/javascripts/jquery.purr.js +1 -1
  89. metadata +92 -175
  90. data/lib/best_in_place/check_version.rb +0 -8
  91. data/spec/spec_helper.rb +0 -23
  92. data/test_app/Gemfile +0 -16
  93. data/test_app/README +0 -256
  94. data/test_app/Rakefile +0 -7
  95. data/test_app/app/assets/javascripts/application.js +0 -35
  96. data/test_app/app/helpers/users_helper.rb +0 -29
  97. data/test_app/config.ru +0 -4
  98. data/test_app/config/application.rb +0 -51
  99. data/test_app/config/boot.rb +0 -13
  100. data/test_app/config/database.yml +0 -22
  101. data/test_app/config/environment.rb +0 -5
  102. data/test_app/config/environments/development.rb +0 -25
  103. data/test_app/config/environments/production.rb +0 -49
  104. data/test_app/config/environments/test.rb +0 -35
  105. data/test_app/config/initializers/backtrace_silencers.rb +0 -7
  106. data/test_app/config/initializers/inflections.rb +0 -10
  107. data/test_app/config/initializers/mime_types.rb +0 -5
  108. data/test_app/config/initializers/secret_token.rb +0 -7
  109. data/test_app/config/initializers/session_store.rb +0 -8
  110. data/test_app/config/locales/en.yml +0 -5
  111. data/test_app/db/migrate/20101206205922_create_users.rb +0 -18
  112. data/test_app/db/migrate/20101212170114_add_receive_email_to_user.rb +0 -9
  113. data/test_app/db/migrate/20110115204441_add_description_to_user.rb +0 -9
  114. data/test_app/db/migrate/20111210084202_add_favorite_color_to_users.rb +0 -5
  115. data/test_app/db/migrate/20111210084251_add_favorite_books_to_users.rb +0 -5
  116. data/test_app/db/migrate/20111217215935_add_birth_date_to_users.rb +0 -5
  117. data/test_app/db/migrate/20111224181356_add_money_to_user.rb +0 -5
  118. data/test_app/db/migrate/20120513003308_create_cars.rb +0 -11
  119. data/test_app/db/migrate/20120607172609_add_favorite_movie_to_users.rb +0 -5
  120. data/test_app/db/migrate/20120616170454_add_money_proc_to_users.rb +0 -6
  121. data/test_app/db/migrate/20120620165212_add_height_to_user.rb +0 -5
  122. data/test_app/db/migrate/20130213224102_add_favorite_locale_to_users.rb +0 -5
  123. data/test_app/db/schema.rb +0 -41
  124. data/test_app/db/seeds.rb +0 -19
  125. data/test_app/doc/README_FOR_APP +0 -2
  126. data/test_app/lib/tasks/.gitkeep +0 -0
  127. data/test_app/lib/tasks/cron.rake +0 -7
  128. data/test_app/public/404.html +0 -26
  129. data/test_app/public/422.html +0 -26
  130. data/test_app/public/500.html +0 -26
  131. data/test_app/public/robots.txt +0 -5
  132. data/test_app/script/rails +0 -6
  133. data/test_app/test/fixtures/users.yml +0 -17
  134. data/test_app/test/functional/users_controller_test.rb +0 -49
  135. data/test_app/test/performance/browsing_test.rb +0 -9
  136. data/test_app/test/test_helper.rb +0 -13
  137. data/test_app/test/unit/helpers/users_helper_test.rb +0 -4
  138. data/test_app/test/unit/user_test.rb +0 -8
  139. data/test_app/vendor/plugins/.gitkeep +0 -0
@@ -1,10 +1,20 @@
1
- //= require jquery.purr
1
+ /*
2
+ * BestInPlace 3.0.0.alpha (2014)
3
+ *
4
+ * Depends:
5
+ * best_in_place.js
6
+ * jquery.purr.js
7
+ */
8
+ /*global BestInPlaceEditor */
2
9
 
3
- jQuery(document).on('best_in_place:error', function(event, request, error) {
10
+ BestInPlaceEditor.defaults.purrErrorContainer = "<span class='bip-flash-error'></span>";
11
+
12
+ jQuery(document).on('best_in_place:error', function (event, request, error) {
13
+ 'use strict';
4
14
  // Display all error messages from server side validation
5
- jQuery.each(jQuery.parseJSON(request.responseText), function(index, value) {
6
- if( typeof(value) == "object") {value = index + " " + value.toString(); }
7
- var container = jQuery("<span class='flash-error'></span>").html(value);
8
- container.purr();
15
+ jQuery.each(jQuery.parseJSON(request.responseText), function (index, value) {
16
+ if (typeof value === "object") {value = index + " " + value.toString(); }
17
+ var container = jQuery(BestInPlaceEditor.defaults.purrErrorContainer).html(value);
18
+ container.purr();
9
19
  });
10
20
  });
data/lib/best_in_place.rb CHANGED
@@ -1,12 +1,32 @@
1
- require "best_in_place/check_version"
2
- require "best_in_place/utils"
3
- require "best_in_place/helper"
4
- require "best_in_place/engine"
5
- require "best_in_place/railtie"
6
- require "best_in_place/controller_extensions"
7
- require "best_in_place/display_methods"
8
- require "action_view"
1
+ require 'rails/railtie'
2
+ require 'action_view/railtie'
3
+ require 'action_controller/railtie'
9
4
 
10
5
  module BestInPlace
11
- autoload :TestHelpers, "best_in_place/test_helpers"
6
+ def self.configure
7
+ @configuration ||= Configuration.new
8
+ yield @configuration if block_given?
9
+ end
10
+
11
+ def self.method_missing(method_name, *args, &block)
12
+ @configuration.respond_to?(method_name) ?
13
+ @configuration.send(method_name, *args, &block) : super
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :container
18
+
19
+ def initialize
20
+ @container = :span
21
+ end
22
+ end
23
+
24
+ configure
12
25
  end
26
+
27
+ require 'best_in_place/engine'
28
+ require 'best_in_place/utils'
29
+ require 'best_in_place/helper'
30
+ require 'best_in_place/railtie'
31
+ require 'best_in_place/controller_extensions'
32
+ require 'best_in_place/display_methods'
@@ -1,28 +1,25 @@
1
1
  module BestInPlace
2
2
  module ControllerExtensions
3
- def respond_with_bip(obj)
4
- obj.changed? ? respond_bip_error(obj) : respond_bip_ok(obj)
3
+ def respond_with_bip(obj, options = {})
4
+ obj.errors.any? ? respond_bip_error(obj) : respond_bip_ok(obj, options)
5
5
  end
6
6
 
7
- private
8
- def respond_bip_ok(obj)
9
- if obj.respond_to?(:id)
10
- klass = "#{obj.class}_#{obj.id}"
11
- else
12
- klass = obj.class.to_s
13
- end
14
- param_key = BestInPlace::Utils.object_to_key(obj)
7
+ private
8
+
9
+ def respond_bip_ok(obj, options = {})
10
+ param_key = options[:param] ||= BestInPlace::Utils.object_to_key(obj)
11
+
15
12
  updating_attr = params[param_key].keys.first
16
13
 
17
- if renderer = BestInPlace::DisplayMethods.lookup(klass, updating_attr)
18
- render :json => renderer.render_json(obj)
14
+ if renderer = BestInPlace::DisplayMethods.lookup(obj.class, updating_attr)
15
+ render json: renderer.render_json(obj)
19
16
  else
20
17
  head :no_content
21
18
  end
22
19
  end
23
20
 
24
21
  def respond_bip_error(obj)
25
- render :json => obj.errors.full_messages, :status => :unprocessable_entity
22
+ render json: obj.errors.full_messages, status: :unprocessable_entity
26
23
  end
27
24
  end
28
25
  end
@@ -1,44 +1,49 @@
1
1
  module BestInPlace
2
- module DisplayMethods
3
- extend self
2
+ module DisplayMethods #:nodoc:
3
+ module_function
4
4
 
5
5
  class Renderer < Struct.new(:opts)
6
6
  def render_json(object)
7
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
- BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]), opts[:helper_options])
13
- else
14
- BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]))
15
- end
16
- {:display_as => value}.to_json
17
- when :proc
18
- {:display_as => opts[:proc].call(object.send(opts[:attr]))}.to_json
19
- else
20
- {}.to_json
8
+ when :model
9
+ { display_as: object.send(opts[:method]) }.to_json
10
+ when :helper
11
+ value = if opts[:helper_options]
12
+ BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]), opts[:helper_options])
13
+ else
14
+ BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]))
15
+ end
16
+ { display_as: value }.to_json
17
+ when :proc
18
+ { display_as: opts[:proc].call(object.send(opts[:attr])) }.to_json
19
+ else
20
+ '{}'
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
- @@table = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
25
+ @@table = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
26
26
 
27
27
  def lookup(klass, attr)
28
- foo = @@table[klass.to_s][attr.to_s]
28
+ foo = model_attributes(klass)[attr.to_s]
29
29
  foo == {} ? nil : foo
30
30
  end
31
31
 
32
32
  def add_model_method(klass, attr, display_as)
33
- @@table[klass.to_s][attr.to_s] = Renderer.new :method => display_as.to_sym, :type => :model
33
+ model_attributes(klass)[attr.to_s] = Renderer.new method: display_as.to_sym, type: :model
34
34
  end
35
35
 
36
36
  def add_helper_method(klass, attr, helper_method, helper_options = nil)
37
- @@table[klass.to_s][attr.to_s] = Renderer.new :method => helper_method.to_sym, :type => :helper, :attr => attr, :helper_options => helper_options
37
+ model_attributes(klass)[attr.to_s] = Renderer.new method: helper_method.to_sym, type: :helper, attr: attr, helper_options: helper_options
38
38
  end
39
-
39
+
40
40
  def add_helper_proc(klass, attr, helper_proc)
41
- @@table[klass.to_s][attr.to_s] = Renderer.new :type => :proc, :attr => attr, :proc => helper_proc
41
+ model_attributes(klass)[attr.to_s] = Renderer.new type: :proc, attr: attr, proc: helper_proc
42
+ end
43
+
44
+ def model_attributes(klass)
45
+ key = Utils.object_to_key(klass)
46
+ @@table[key]
42
47
  end
43
48
  end
44
49
  end
@@ -1,7 +1,7 @@
1
1
  module BestInPlace
2
2
  class Engine < Rails::Engine
3
- initializer "setup for rails" do
4
- ActionView::Base.send(:include, BestInPlace::BestInPlaceHelpers)
3
+ initializer 'best_in_place' do
4
+ ActionView::Base.send(:include, BestInPlace::Helper)
5
5
  ActionController::Base.send(:include, BestInPlace::ControllerExtensions)
6
6
  end
7
7
  end
@@ -1,99 +1,90 @@
1
1
  module BestInPlace
2
- module BestInPlaceHelpers
3
-
2
+ module Helper
4
3
  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
4
+ best_in_place_assert_arguments(opts)
5
+ type = opts[:as] || :input
6
+ field = field.to_s
8
7
 
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]}"
8
+ options = {}
9
+ options[:data] = HashWithIndifferentAccess.new(opts[:data])
10
+ options[:data]['bip-type'] = type
11
+ options[:data]['bip-attribute'] = field
12
+
13
+ real_object = best_in_place_real_object_for object
14
+
15
+ display_value = best_in_place_build_value_for(real_object, field, opts)
16
+
17
+ value = real_object.send(field)
18
+
19
+ if opts[:collection] or type == :checkbox
20
+ collection = opts[:collection]
21
+ case type
22
+ when :checkbox
23
+ value = value.to_s
24
+ if collection.blank?
25
+ collection = best_in_place_default_collection
26
+ else
27
+ collection = best_in_place_collection_builder(collection)
28
+ end
29
+ display_value = collection[value]
30
+ collection = collection.to_json
31
+ else # :select
32
+ value = value.to_s
33
+ collection = best_in_place_collection_builder(collection)
34
+ display_value = collection[value]
35
+ collection = collection.to_json
36
+ end
37
+ options[:data]['bip-collection'] = html_escape(collection)
11
38
  end
12
39
 
13
- real_object = real_object_for object
14
- opts[:type] ||= :input
15
- opts[:collection] ||= []
16
- field = field.to_s
40
+ options[:class] = ['best_in_place'] + Array(opts[:class] || opts[:classes])
41
+ options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field)
17
42
 
18
- display_value = build_value_for(real_object, field, opts)
43
+ options[:data]['bip-activator'] = opts[:activator].presence
19
44
 
20
- collection = nil
21
- value = nil
22
- if opts[:type] == :select && !opts[:collection].blank?
23
- value = real_object.send(field)
24
- display_value = Hash[opts[:collection]].stringify_keys[value.to_s]
25
- collection = opts[:collection].to_json
26
- end
27
- if opts[:type] == :checkbox
28
- value = !!real_object.send(field)
29
- if opts[:collection].blank? || opts[:collection].size != 2
30
- opts[:collection] = ["No", "Yes"]
31
- end
32
- display_value = value ? opts[:collection][1] : opts[:collection][0]
33
- collection = opts[:collection].to_json
34
- end
35
- classes = ["best_in_place"]
36
- unless opts[:classes].nil?
37
- # the next three lines enable this opt to handle both a stings and a arrays
38
- classes << opts[:classes]
39
- classes.flatten!
40
- end
45
+ options[:data]['bip-html-attrs'] = opts[:html_attrs].to_json unless opts[:html_attrs].blank?
46
+ options[:data]['bip-inner-class'] = opts[:inner_class].presence
41
47
 
42
- out = "<span class='#{classes.join(" ")}'"
43
- out << " id='#{BestInPlace::Utils.build_best_in_place_id(real_object, field)}'"
44
- out << " data-url='#{opts[:path].blank? ? url_for(object) : url_for(opts[:path])}'"
45
- out << " data-object='#{opts[:object_name] || BestInPlace::Utils.object_to_key(real_object)}'"
46
- out << " data-collection='#{attribute_escape(collection)}'" unless collection.blank?
47
- out << " data-attribute='#{field}'"
48
- out << " data-activator='#{opts[:activator]}'" unless opts[:activator].blank?
49
- out << " data-ok-button='#{opts[:ok_button]}'" unless opts[:ok_button].blank?
50
- out << " data-ok-button-class='#{opts[:ok_button_class]}'" unless opts[:ok_button_class].blank?
51
- out << " data-cancel-button='#{opts[:cancel_button]}'" unless opts[:cancel_button].blank?
52
- out << " data-cancel-button-class='#{opts[:cancel_button_class]}'" unless opts[:cancel_button_class].blank?
53
- out << " data-nil='#{attribute_escape(opts[:nil])}'" unless opts[:nil].blank?
54
- out << " data-use-confirm='#{opts[:use_confirm]}'" unless opts[:use_confirm].nil?
55
- out << " data-type='#{opts[:type]}'"
56
- out << " data-inner-class='#{opts[:inner_class]}'" if opts[:inner_class]
57
- out << " data-html-attrs='#{opts[:html_attrs].to_json}'" unless opts[:html_attrs].blank?
58
- out << " data-original-content='#{attribute_escape(real_object.send(field))}'" if opts[:display_as] || opts[:display_with]
59
- out << " data-value='#{attribute_escape(value)}'" if value
60
-
61
- if opts[:data] && opts[:data].is_a?(Hash)
62
- opts[:data].each do |k, v|
63
- if !v.is_a?(String) && !v.is_a?(Symbol)
64
- v = v.to_json
65
- end
66
- out << %( data-#{k.to_s.dasherize}="#{v}")
67
- end
68
- end
69
- if !opts[:sanitize].nil? && !opts[:sanitize]
70
- out << " data-sanitize='false'>"
71
- out << display_value.to_s
72
- else
73
- out << ">#{h(display_value.to_s)}"
48
+ options[:data]['bip-placeholder'] = html_escape(opts[:place_holder]).presence
49
+
50
+ options[:data]['bip-object'] = opts[:param] || BestInPlace::Utils.object_to_key(real_object)
51
+ options[:data]['bip-ok-button'] = opts[:ok_button].presence
52
+ options[:data]['bip-ok-button-class'] = opts[:ok_button_class].presence
53
+ options[:data]['bip-cancel-button'] = opts[:cancel_button].presence
54
+ options[:data]['bip-cancel-button-class'] = opts[:cancel_button_class].presence
55
+ options[:data]['bip-original-content'] = html_escape(opts[:value] || value).presence
56
+
57
+ options[:data]['bip-url'] = url_for(opts[:url] || object)
58
+
59
+ options[:data]['bip-confirm'] = opts[:confirm].presence
60
+ options[:data]['bip-value'] = html_escape(value).presence
61
+
62
+ if opts[:raw]
63
+ options[:data]['bip-raw'] = 'true'
74
64
  end
75
- out << "</span>"
76
- raw out
65
+
66
+ # delete nil keys only
67
+ options[:data].delete_if { |_, v| v.nil? }
68
+ container = opts[:container] || BestInPlace.container
69
+ content_tag(container, display_value, options, opts[:raw].blank?)
77
70
  end
78
71
 
79
- def best_in_place_if(condition, object, field, opts={})
72
+ def best_in_place_if(condition, object, field, opts = {})
80
73
  if condition
81
74
  best_in_place(object, field, opts)
82
75
  else
83
- build_value_for real_object_for(object), field, opts
76
+ best_in_place_build_value_for best_in_place_real_object_for(object), field, opts
84
77
  end
85
78
  end
86
79
 
87
- private
88
- def build_value_for(object, field, opts)
89
- return "" if object.send(field).blank?
80
+ def best_in_place_unless(condition, object, field, opts = {})
81
+ best_in_place_if(!condition, object, field, opts)
82
+ end
90
83
 
91
- klass = if object.respond_to?(:id)
92
- "#{object.class}_#{object.id}"
93
- else
94
- object.class.to_s
95
- end
84
+ private
96
85
 
86
+ def best_in_place_build_value_for(object, field, opts)
87
+ klass = object.class
97
88
  if opts[:display_as]
98
89
  BestInPlace::DisplayMethods.add_model_method(klass, field, opts[:display_as])
99
90
  object.send(opts[:display_as]).to_s
@@ -107,7 +98,15 @@ module BestInPlace
107
98
  if opts[:helper_options]
108
99
  BestInPlace::ViewHelpers.send(opts[:display_with], object.send(field), opts[:helper_options])
109
100
  else
110
- BestInPlace::ViewHelpers.send(opts[:display_with], object.send(field))
101
+ field_value = object.send(field)
102
+
103
+ if field_value.blank?
104
+ ''
105
+ else
106
+
107
+
108
+ BestInPlace::ViewHelpers.send(opts[:display_with], field_value)
109
+ end
111
110
  end
112
111
 
113
112
  else
@@ -115,18 +114,77 @@ module BestInPlace
115
114
  end
116
115
  end
117
116
 
118
- def attribute_escape(data)
119
- return unless data
117
+ def best_in_place_real_object_for(object)
118
+ (object.is_a?(Array) && object.last.class.respond_to?(:model_name)) ? object.last : object
119
+ end
120
120
 
121
- data.to_s.
122
- gsub("&", "&amp;").
123
- gsub("'", "&apos;").
124
- gsub(/\r?\n/, "&#10;")
121
+ def best_in_place_assert_arguments(args)
122
+ args.assert_valid_keys(:id, :type, :nil, :classes, :collection, :data,
123
+ :activator, :cancel_button, :cancel_button_class, :html_attrs, :inner_class, :nil,
124
+ :object_name, :ok_button, :ok_button_class, :display_as, :display_with, :path, :value,
125
+ :use_confirm, :confirm, :sanitize, :raw, :helper_options, :url, :place_holder, :class,
126
+ :as, :param, :container)
127
+
128
+ best_in_place_deprecated_options(args)
129
+
130
+ if args[:display_as] && args[:display_with]
131
+ fail ArgumentError, 'Can`t use both `display_as`` and `display_with` options at the same time'
132
+ end
133
+
134
+ if args[:display_with] && !args[:display_with].is_a?(Proc) && !ViewHelpers.respond_to?(args[:display_with])
135
+ fail ArgumentError, "Can't find helper #{args[:display_with]}"
136
+ end
125
137
  end
126
138
 
127
- def real_object_for(object)
128
- (object.is_a?(Array) && object.last.class.respond_to?(:model_name)) ? object.last : object
139
+ def best_in_place_deprecated_options(args)
140
+ if deprecated_option = args.delete(:path)
141
+ args[:url] = deprecated_option
142
+ ActiveSupport::Deprecation.warn('[Best_in_place] :path is deprecated in favor of :url ')
143
+ end
144
+
145
+ if deprecated_option = args.delete(:object_name)
146
+ args[:param] = deprecated_option
147
+ ActiveSupport::Deprecation.warn('[Best_in_place] :object_name is deprecated in favor of :param ')
148
+ end
149
+
150
+ if deprecated_option = args.delete(:type)
151
+ args[:as] = deprecated_option
152
+ ActiveSupport::Deprecation.warn('[Best_in_place] :type is deprecated in favor of :as ')
153
+ end
154
+
155
+ if deprecated_option = args.delete(:classes)
156
+ args[:class] = deprecated_option
157
+ ActiveSupport::Deprecation.warn('[Best_in_place] :classes is deprecated in favor of :class ')
158
+ end
159
+
160
+ if deprecated_option = args.delete(:nil)
161
+ args[:place_holder] = deprecated_option
162
+ ActiveSupport::Deprecation.warn('[Best_in_place] :nil is deprecated in favor of :place_holder ')
163
+ end
164
+
165
+ if deprecated_option = args.delete(:use_confirm)
166
+ args[:confirm] = deprecated_option
167
+ ActiveSupport::Deprecation.warn('[Best_in_place] :use_confirm is deprecated in favor of :confirm ')
168
+ end
169
+
170
+ if deprecated_option = args.delete(:sanitize)
171
+ args[:raw] = !deprecated_option
172
+ ActiveSupport::Deprecation.warn('[Best_in_place] :sanitize is deprecated in favor of :raw ')
173
+ end
174
+ end
175
+
176
+ def best_in_place_collection_builder(collection)
177
+ case collection
178
+ when Array
179
+ Hash[collection.map { |x| [x.to_s, x.to_s] }]
180
+ else
181
+ collection.stringify_keys
182
+ end
183
+ end
184
+
185
+ def best_in_place_default_collection
186
+ {'true' => t(:'best_in_place.yes', default: 'Yes'),
187
+ 'false' => t(:'best_in_place.no', default: 'No')}
129
188
  end
130
189
  end
131
190
  end
132
-