mvcoffee-rails 1.0.0

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,146 @@
1
+ require 'mvcoffee/rails/engine'
2
+ require "mvcoffee/rails/version"
3
+ require "mvcoffee/rails/active_record"
4
+
5
+ require 'mvcoffee/mvcoffee'
6
+
7
+
8
+ # This probably doesn't belong in this file, but if I try to do it in a file called
9
+ # anything having to do with "action_controller", it confuses the whole Rails system
10
+ # and nothing works. It works the way I want it to in this file, so I'm not messin'
11
+ # with it! Not elegant, but if it ain't broke....
12
+ module ActionController
13
+ class Base
14
+ # Set up "around aliases". Rename the render and redirect methods to something
15
+ # that is guaranteed not to clash with anything. I use these aliases below in the
16
+ # render_mvcoffee method.
17
+ alias :_mvcoffee_alias_render :render
18
+ alias :_mvcoffee_alias_redirect_to :redirect_to
19
+
20
+ # Call this class macro in your application_controller.rb file if you want to
21
+ # do the monkeypatch for the whole application.
22
+ #
23
+ # I could make it so this happens by default, but I think it should require an
24
+ # explicit action on the programmer's part to make a monkeypatch with
25
+ # potentially unintended consequences happen. This is in keeping with the
26
+ # principle of Least Surprise. If something goes wonky after you called this
27
+ # method, well, you know you called it, so you can comment it out (and probably
28
+ # curse me online) Let's hope it never comes to that....
29
+ def self.monkeypatch_mvcoffee
30
+
31
+ # The really cool thing about monkeypatching this method is that it gets called
32
+ # automagically in your controllers at the end of each action if the action falls
33
+ # through. So, with this in place, with no extra action required by the
34
+ # programmer, the MVCoffee thing will just happen. This means you can do a
35
+ # redirect even on a json request without even thinking about it!
36
+ define_method :render do |action = nil, opts = {}|
37
+ # Uncomment this is you want to prove it to yourself that it really is doing
38
+ # the around alias.
39
+ # puts "!!!! Doing monkeypatched render !!!!!"
40
+ render_mvcoffee action, opts
41
+ end
42
+
43
+ # And the cool thing about monkeypatching this method is that you can call it
44
+ # indiscriminately without doing that format...do business checking for json
45
+ # and avoiding redirecting if it is json. Just go ahead and call redirect_to,
46
+ # and the client framework will do the right thing, handling the redirect AFTER
47
+ # processing the json fetched over ajax.
48
+ define_method :redirect_to do |action, opts = {}|
49
+ # Uncomment this is you want to prove it to yourself that it really is doing
50
+ # the around alias.
51
+ # puts "!!!! Doing monkeypatched redirect_to !!!!!"
52
+ @mvcoffee.set_redirect action, opts
53
+ render_mvcoffee
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+
62
+ # MVCoffee is a module that should be included at the top of your ApplicationController
63
+ # just below this line:
64
+ # class ApplicationController < ActionController::Base
65
+ #
66
+ # It will add two methods to all controllers:
67
+ #
68
+ # * is_full_page_load? which will return true if it detects a full page load has occurred,
69
+ # meaning the client-side MVCoffee data store has been wiped out. Otherwise you can
70
+ # assume the client-side cache is still intact.
71
+ #
72
+ # * render_mvcoffee which should be called instead of `render` or `redirect_to` in any
73
+ # action that relies on MVCoffee to handle things on the client side (refresh the cache,
74
+ # handle a redirect on the client, or simply update via ajax). It will handle doing the
75
+ # right thing whether the request is html or json (saving you that format verbosity).
76
+ #
77
+ # It will also declare an instance variable available to all controllers, `@mvcoffee`,
78
+ # an instance of an MVCoffee::MVCoffee object. All controller actions that use
79
+ # MVCoffee should make modifications to this object (using methods such as
80
+ # `set_model_data` and `set_session`, etc.) before calling `render_mvcoffee`, then it
81
+ # will automatically be sent to the client as json.
82
+ #
83
+ # Author:: Kirk Bowers (mailto:kirkbowers@yahoo.com)
84
+ # Copyright:: Copyright (c) 2014 Kirk Bowers
85
+ # License:: MIT License
86
+ module MVCoffee
87
+
88
+ # Does it's best attempt at determining whether the incoming request happened over
89
+ # explicit ajax, implicit ajax (via turbolinks), or if it is a full page load.
90
+ # Returns true if it looks like the request is a full page load, meaning the client
91
+ # side javascript session is going to be started over fresh and all cached data will
92
+ # be wiped out.
93
+ def is_full_page_load?
94
+ !request.xhr? and !request.headers["HTTP_X_XHR_REFERER"]
95
+ end
96
+
97
+ # Handles rendering for actions that rely on MVCoffee on the client. It will send
98
+ # the `@mvcoffee` object to the client serialized as json, and do the right thing
99
+ # based on whether the request is html or json.
100
+ #
101
+ # If it is an html request, and there is a redirect issued in the `@mvcoffee` object,
102
+ # this method will issue a hard redirect from the server (which will
103
+ # wipe out the cache on the client). This is desirable for actions that only want
104
+ # to maintain the session when the request is over json.
105
+ def render_mvcoffee(action = nil, opts = {})
106
+ respond_to do |format|
107
+ format.html {
108
+ if @mvcoffee.redirect
109
+ if opts == {} and action.respond_to? :to_hash
110
+ opts = action
111
+ end
112
+
113
+ _mvcoffee_alias_redirect_to @mvcoffee.redirect, @mvcoffee.flash.merge(opts)
114
+ else
115
+ _mvcoffee_alias_render action, opts
116
+ end
117
+ }
118
+ format.json { _mvcoffee_alias_render json: @mvcoffee.to_json }
119
+ end
120
+ end
121
+
122
+
123
+ # If this module is included into the application controller, this will fire for
124
+ # all controllers and instantiate the `@mvcoffee` object.
125
+ ::ActionController::Base::before_action do
126
+ # Glean the client session from the cookie.
127
+ session = cookies['mvcoffee_session']
128
+ client_session = {}
129
+
130
+ Rails.logger.info "-- MVCoffee -- Received Client Session #{session}"
131
+
132
+ if session
133
+ client_session = CGI.parse(session)
134
+ end
135
+
136
+ # reset the cookie
137
+ cookies['mvcoffee_session'] = { value: "", path: "/" }
138
+
139
+ # We have to go back to the root with the leading :: since this module most likely
140
+ # has been include'd into the controllers performing this before_action
141
+ @mvcoffee = ::MVCoffee::MVCoffee.new client_session
142
+
143
+ end
144
+
145
+ end
146
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mvcoffee-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kirk Bowers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.16
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.16
55
+ description: "<p><code>mvcoffee-rails</code> is a Rails gem that makes it easy to
56
+ use the \n<a href=\"https://github.com/kirkbowers/mvcoffee\">MVCoffee</a> client-side
57
+ MVC framework in your\nRails project.</p>\n\n<p>The full documentation for this
58
+ gem is at <a href=\"http://mvcoffee.org/mvcoffee-rails\">mvcoffee.org</a>.</p>\n"
59
+ email:
60
+ - kirkbowers@yahoo.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - app/assets/javascripts/mvcoffee.js
66
+ - app/assets/javascripts/mvcoffee.min.js
67
+ - app/helpers/mvcoffee_helper.rb
68
+ - lib/mvcoffee-rails.rb
69
+ - lib/mvcoffee/mvcoffee.rb
70
+ - lib/mvcoffee/rails/active_record.rb
71
+ - lib/mvcoffee/rails/engine.rb
72
+ - lib/mvcoffee/rails/version.rb
73
+ homepage: https://github.com/kirkbowers/mvcoffee-rails
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.3.6
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: The server-side Rails gem that makes it easy to use the MVCoffee client-side
97
+ MVC framework in your Rails project. Full documentation is at mvcoffee.org.
98
+ test_files: []