best_in_place_rails_4 0.0.1

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.
@@ -0,0 +1,10 @@
1
+ //= require jquery.purr
2
+
3
+ jQuery(document).on('best_in_place:error', function(event, request, error) {
4
+ // 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();
9
+ });
10
+ });
@@ -0,0 +1,161 @@
1
+ /**
2
+ * jquery.purr.js
3
+ * Copyright (c) 2008 Net Perspective (net-perspective.com)
4
+ * Licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
5
+ *
6
+ * @author R.A. Ray
7
+ * @projectDescription jQuery plugin for dynamically displaying unobtrusive messages in the browser. Mimics the behavior of the MacOS program "Growl."
8
+ * @version 0.1.0
9
+ *
10
+ * @requires jquery.js (tested with 1.2.6)
11
+ *
12
+ * @param fadeInSpeed int - Duration of fade in animation in miliseconds
13
+ * default: 500
14
+ * @param fadeOutSpeed int - Duration of fade out animationin miliseconds
15
+ default: 500
16
+ * @param removeTimer int - Timeout, in miliseconds, before notice is removed once it is the top non-sticky notice in the list
17
+ default: 4000
18
+ * @param isSticky bool - Whether the notice should fade out on its own or wait to be manually closed
19
+ default: false
20
+ * @param usingTransparentPNG bool - Whether or not the notice is using transparent .png images in its styling
21
+ default: false
22
+ */
23
+
24
+ (function(jQuery) {
25
+
26
+ jQuery.purr = function(notice, options)
27
+ {
28
+ // Convert notice to a jQuery object
29
+ notice = jQuery(notice);
30
+
31
+ // Add a class to denote the notice as not sticky
32
+ notice.addClass('purr');
33
+
34
+ // Get the container element from the page
35
+ var cont = document.getElementById('purr-container');
36
+
37
+ // If the container doesn't yet exist, we need to create it
38
+ if (!cont)
39
+ {
40
+ cont = '<div id="purr-container"></div>';
41
+ }
42
+
43
+ // Convert cont to a jQuery object
44
+ cont = jQuery(cont);
45
+
46
+ // Add the container to the page
47
+ jQuery('body').append(cont);
48
+
49
+ notify();
50
+
51
+ function notify ()
52
+ {
53
+ // Set up the close button
54
+ var close = document.createElement('a');
55
+ jQuery(close).attr({
56
+ className: 'close',
57
+ href: '#close'
58
+ }).appendTo(notice).click(function() {
59
+ removeNotice();
60
+ return false;
61
+ });
62
+
63
+ // If ESC is pressed remove notice
64
+ jQuery(document).keyup(function(e) {
65
+ if (e.keyCode == 27) {
66
+ removeNotice();
67
+ }
68
+ });
69
+
70
+ // Add the notice to the page and keep it hidden initially
71
+ notice.appendTo(cont).hide();
72
+
73
+ if (jQuery.browser.msie && options.usingTransparentPNG)
74
+ {
75
+ // IE7 and earlier can't handle the combination of opacity and transparent pngs, so if we're using transparent pngs in our
76
+ // notice style, we'll just skip the fading in.
77
+ notice.show();
78
+ }
79
+ else
80
+ {
81
+ //Fade in the notice we just added
82
+ notice.fadeIn(options.fadeInSpeed);
83
+ }
84
+
85
+ // Set up the removal interval for the added notice if that notice is not a sticky
86
+ if (!options.isSticky)
87
+ {
88
+ var topSpotInt = setInterval(function() {
89
+ // Check to see if our notice is the first non-sticky notice in the list
90
+ if (notice.prevAll('.purr').length == 0)
91
+ {
92
+ // Stop checking once the condition is met
93
+ clearInterval(topSpotInt);
94
+
95
+ // Call the close action after the timeout set in options
96
+ setTimeout(function() {
97
+ removeNotice();
98
+ }, options.removeTimer);
99
+ }
100
+ }, 200);
101
+ }
102
+ }
103
+
104
+ function removeNotice()
105
+ {
106
+ // IE7 and earlier can't handle the combination of opacity and transparent pngs, so if we're using transparent pngs in our
107
+ // notice style, we'll just skip the fading out.
108
+ if (jQuery.browser.msie && options.usingTransparentPNG)
109
+ {
110
+ notice.css({ opacity: 0 }).animate({ height: '0px'},
111
+ {
112
+ duration: options.fadeOutSpeed,
113
+ complete: function ()
114
+ {
115
+ notice.remove();
116
+ }
117
+ }
118
+ );
119
+ }
120
+ else
121
+ {
122
+ // Fade the object out before reducing its height to produce the sliding effect
123
+ notice.animate({ opacity: '0' },
124
+ {
125
+ duration: options.fadeOutSpeed,
126
+ complete: function ()
127
+ {
128
+ notice.animate({ height: '0px' },
129
+ {
130
+ duration: options.fadeOutSpeed,
131
+ complete: function()
132
+ {
133
+ notice.remove();
134
+ }
135
+ }
136
+ );
137
+ }
138
+ }
139
+ );
140
+ }
141
+ };
142
+ };
143
+
144
+ jQuery.fn.purr = function(options)
145
+ {
146
+ options = options || {};
147
+ options.fadeInSpeed = options.fadeInSpeed || 500;
148
+ options.fadeOutSpeed = options.fadeOutSpeed || 500;
149
+ options.removeTimer = options.removeTimer || 4000;
150
+ options.isSticky = options.isSticky || false;
151
+ options.usingTransparentPNG = options.usingTransparentPNG || false;
152
+
153
+ this.each(function()
154
+ {
155
+ new jQuery.purr( this, options );
156
+ }
157
+ );
158
+
159
+ return this;
160
+ };
161
+ })( jQuery );
@@ -0,0 +1,11 @@
1
+ require "best_in_place_rails_4/check_version"
2
+ require "best_in_place_rails_4/utils"
3
+ require "best_in_place_rails_4/helper"
4
+ require "best_in_place_rails_4/engine"
5
+ require "best_in_place_rails_4/railtie"
6
+ require "best_in_place_rails_4/controller_extensions"
7
+ require "best_in_place_rails_4/display_methods"
8
+ require "action_view"
9
+
10
+ module BestInPlaceRails4
11
+ end
@@ -0,0 +1,8 @@
1
+ module BestInPlaceRails4
2
+ module CheckVersion
3
+ if Rails::VERSION::STRING < "4.0"
4
+ raise "This version of Best in Place is intended to be used for Rails >= 4.0."
5
+ return
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ module BestInPlaceRails4
2
+ module ControllerExtensions
3
+ def respond_with_bip(obj)
4
+ obj.changed? ? respond_bip_error(obj) : respond_bip_ok(obj)
5
+ end
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 = BestInPlaceRails4::Utils.object_to_key(obj)
15
+ updating_attr = params[param_key].keys.first
16
+
17
+ if renderer = BestInPlaceRails4::DisplayMethods.lookup(klass, updating_attr)
18
+ render :json => renderer.render_json(obj)
19
+ else
20
+ head :ok
21
+ end
22
+ end
23
+
24
+ def respond_bip_error(obj)
25
+ render :json => obj.errors.full_messages, :status => :unprocessable_entity
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,44 @@
1
+ module BestInPlaceRails4
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
+ BestInPlaceRails4::ViewHelpers.send(opts[:method], object.send(opts[:attr]), opts[:helper_options])
13
+ else
14
+ BestInPlaceRails4::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
21
+ end
22
+ end
23
+ end
24
+
25
+ @@table = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
26
+
27
+ def lookup(klass, attr)
28
+ foo = @@table[klass.to_s][attr.to_s]
29
+ foo == {} ? nil : foo
30
+ end
31
+
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
34
+ end
35
+
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
38
+ end
39
+
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
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,8 @@
1
+ module BestInPlaceRails4
2
+ class Engine < Rails::Engine
3
+ initializer "setup for rails" do
4
+ ActionView::Base.send(:include, BestInPlaceRails4::BestInPlaceHelpers)
5
+ ActionController::Base.send(:include, BestInPlaceRails4::ControllerExtensions)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,128 @@
1
+ module BestInPlaceRails4
2
+ module BestInPlaceHelpers
3
+
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
+
13
+ real_object = real_object_for object
14
+ opts[:type] ||= :input
15
+ opts[:collection] ||= []
16
+ field = field.to_s
17
+
18
+ display_value = build_value_for(real_object, field, opts)
19
+
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
41
+
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-cancel-button='#{opts[:cancel_button]}'" unless opts[:cancel_button].blank?
51
+ out << " data-nil='#{attribute_escape(opts[:nil])}'" unless opts[:nil].blank?
52
+ out << " data-use-confirm='#{opts[:use_confirm]}'" unless opts[:use_confirm].nil?
53
+ out << " data-type='#{opts[:type]}'"
54
+ out << " data-inner-class='#{opts[:inner_class]}'" if opts[:inner_class]
55
+ out << " data-html-attrs='#{opts[:html_attrs].to_json}'" unless opts[:html_attrs].blank?
56
+ out << " data-original-content='#{attribute_escape(real_object.send(field))}'" if opts[:display_as] || opts[:display_with]
57
+ out << " data-value='#{attribute_escape(value)}'" if value
58
+
59
+ if opts[:data] && opts[:data].is_a?(Hash)
60
+ opts[:data].each do |k, v|
61
+ if !v.is_a?(String) && !v.is_a?(Symbol)
62
+ v = v.to_json
63
+ end
64
+ out << %( data-#{k.to_s.dasherize}="#{v}")
65
+ end
66
+ end
67
+ if !opts[:sanitize].nil? && !opts[:sanitize]
68
+ out << " data-sanitize='false'>"
69
+ out << display_value.to_s
70
+ else
71
+ out << ">#{h(display_value.to_s)}"
72
+ end
73
+ out << "</span>"
74
+ raw out
75
+ end
76
+
77
+ def best_in_place_if(condition, object, field, opts={})
78
+ if condition
79
+ best_in_place(object, field, opts)
80
+ else
81
+ build_value_for real_object_for(object), field, opts
82
+ end
83
+ end
84
+
85
+ private
86
+ def build_value_for(object, field, opts)
87
+ return "" if object.send(field).blank?
88
+
89
+ if (object.respond_to?(:id))
90
+ klass = "#{object.class}_#{object.id}"
91
+ else
92
+ klass = object.class.to_s
93
+ end
94
+ if opts[:display_as]
95
+ BestInPlace::DisplayMethods.add_model_method(klass, field, opts[:display_as])
96
+ object.send(opts[:display_as]).to_s
97
+
98
+ elsif opts[:display_with].try(:is_a?, Proc)
99
+ BestInPlace::DisplayMethods.add_helper_proc(klass, field, opts[:display_with])
100
+ opts[:display_with].call(object.send(field))
101
+ elsif opts[:display_with]
102
+ BestInPlace::DisplayMethods.add_helper_method(klass, field, opts[:display_with], opts[:helper_options])
103
+ if opts[:helper_options]
104
+ BestInPlace::ViewHelpers.send(opts[:display_with], object.send(field), opts[:helper_options])
105
+ else
106
+ BestInPlace::ViewHelpers.send(opts[:display_with], object.send(field))
107
+ end
108
+
109
+ else
110
+ object.send(field).to_s.presence || ""
111
+ end
112
+ end
113
+
114
+ def attribute_escape(data)
115
+ return unless data
116
+
117
+ data.to_s.
118
+ gsub("&", "&amp;").
119
+ gsub("'", "&apos;").
120
+ gsub(/\r?\n/, "&#10;")
121
+ end
122
+
123
+ def real_object_for(object)
124
+ (object.is_a?(Array) && object.last.class.respond_to?(:model_name)) ? object.last : object
125
+ end
126
+ end
127
+ end
128
+
@@ -0,0 +1,7 @@
1
+ module BestInPlaceRails4
2
+ class Railtie < Rails::Railtie
3
+ initializer "set view helpers" do
4
+ BestInPlaceRails4::ViewHelpers = ActionView::Base.new
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module BestInPlaceRails4
2
+ module Utils
3
+ extend self
4
+
5
+ def build_best_in_place_id(object, field)
6
+ if object.is_a?(Symbol) || object.is_a?(String)
7
+ return "best_in_place_#{object}_#{field}"
8
+ end
9
+
10
+ id = "best_in_place_#{object_to_key(object)}"
11
+ id << "_#{object.id}" if object.class.ancestors.include?(ActiveModel::Serializers::JSON)
12
+ id << "_#{field}"
13
+ id
14
+ end
15
+
16
+ def object_to_key(object)
17
+ return object.class.to_s.underscore unless object.class.respond_to?(:model_name)
18
+ ActiveModel::Naming.param_key(object.class)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module BestInPlaceRails4
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: best_in_place_rails_4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Berry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jquery-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: BestInPlace is a jQuery script and a Rails 3 helper that provide the
70
+ method best_in_place to display any object field easily editable for the user by
71
+ just clicking on it. It supports input data, text data, boolean data and custom
72
+ dropdown data. It works with RESTful controllers.
73
+ email:
74
+ - cavneb@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - .gitignore
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - best_in_place_rails_4.gemspec
85
+ - lib/assets/javascripts/best_in_place.js
86
+ - lib/assets/javascripts/best_in_place.purr.js
87
+ - lib/assets/javascripts/jquery.purr.js
88
+ - lib/best_in_place_rails_4.rb
89
+ - lib/best_in_place_rails_4/check_version.rb
90
+ - lib/best_in_place_rails_4/controller_extensions.rb
91
+ - lib/best_in_place_rails_4/display_methods.rb
92
+ - lib/best_in_place_rails_4/engine.rb
93
+ - lib/best_in_place_rails_4/helper.rb
94
+ - lib/best_in_place_rails_4/railtie.rb
95
+ - lib/best_in_place_rails_4/utils.rb
96
+ - lib/best_in_place_rails_4/version.rb
97
+ homepage: http://github.com/cavneb/best_in_place
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.0.14
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: It makes any field in place editable by clicking on it, it works for inputs,
121
+ textareas, select dropdowns and checkboxes
122
+ test_files: []