tila 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +44 -0
  4. data/Rakefile +34 -0
  5. data/lib/tasks/tila_tasks.rake +4 -0
  6. data/lib/tila.rb +14 -0
  7. data/lib/tila/actionable.rb +43 -0
  8. data/lib/tila/form_handler.rb +31 -0
  9. data/lib/tila/messages.rb +12 -0
  10. data/lib/tila/modelable.rb +28 -0
  11. data/lib/tila/objects.rb +10 -0
  12. data/lib/tila/params.rb +21 -0
  13. data/lib/tila/resource_loaders.rb +43 -0
  14. data/lib/tila/resourceful.rb +42 -0
  15. data/lib/tila/resourceful_urls.rb +22 -0
  16. data/lib/tila/save_destroy.rb +14 -0
  17. data/lib/tila/version.rb +3 -0
  18. data/test/dummy/README.rdoc +28 -0
  19. data/test/dummy/Rakefile +6 -0
  20. data/test/dummy/app/assets/javascripts/application.js +13 -0
  21. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  22. data/test/dummy/app/controllers/application_controller.rb +5 -0
  23. data/test/dummy/app/helpers/application_helper.rb +2 -0
  24. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/test/dummy/bin/bundle +3 -0
  26. data/test/dummy/bin/rails +4 -0
  27. data/test/dummy/bin/rake +4 -0
  28. data/test/dummy/bin/setup +29 -0
  29. data/test/dummy/config.ru +4 -0
  30. data/test/dummy/config/application.rb +26 -0
  31. data/test/dummy/config/boot.rb +5 -0
  32. data/test/dummy/config/database.yml +25 -0
  33. data/test/dummy/config/environment.rb +5 -0
  34. data/test/dummy/config/environments/development.rb +41 -0
  35. data/test/dummy/config/environments/production.rb +79 -0
  36. data/test/dummy/config/environments/test.rb +42 -0
  37. data/test/dummy/config/initializers/assets.rb +11 -0
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  40. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/test/dummy/config/initializers/inflections.rb +16 -0
  42. data/test/dummy/config/initializers/mime_types.rb +4 -0
  43. data/test/dummy/config/initializers/session_store.rb +3 -0
  44. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  45. data/test/dummy/config/locales/en.yml +23 -0
  46. data/test/dummy/config/routes.rb +56 -0
  47. data/test/dummy/config/secrets.yml +22 -0
  48. data/test/dummy/public/404.html +67 -0
  49. data/test/dummy/public/422.html +67 -0
  50. data/test/dummy/public/500.html +66 -0
  51. data/test/dummy/public/favicon.ico +0 -0
  52. data/test/test_helper.rb +20 -0
  53. data/test/tila_test.rb +7 -0
  54. metadata +160 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6f8a260763517d729f728c1d7598f246c99bdd8
4
+ data.tar.gz: 9b6b93fcc8be2b7deb4a4de95cdfbdb92966aa27
5
+ SHA512:
6
+ metadata.gz: 0e9a4a1d61bd734ed8a8f3be2e4993f2567d46438d5139754afa2692842b42e291897ca6d6fc12166c1f94a28217884d03da07ac4324ed7582b7ddbbb146201c
7
+ data.tar.gz: d50ee02a1b6cf40467516809b51fb9d520cf63c0f045258242cf2fe122e629c01135d9912874107cd06e9b58ab854e1cef165532552b2f921e8eff1df813e79f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Rolf van de Krol
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Tila
2
+
3
+ Most Rails projects contain a lot of controllers that are quite repetitive,
4
+ because most controllers essentially what is needed to get a simple CRUD
5
+ interface. This project contains components that can be used to build the
6
+ controller of this simple CRUD interface.
7
+
8
+ Because Tila favors composition over inheritance, is allows for a flexibility
9
+ that most other projects in this space don't have. If you only want to use a
10
+ part of Tila, that works perfectly fine.
11
+
12
+ ## Components
13
+
14
+ * __Actionable__: Provides an `action` helper so we can simply find out which
15
+ action is called and contains a list of actions that are operated on
16
+ collection and which actions are actions that save the model instance. It also
17
+ provides simple helpers to find out if the current action is in one of the
18
+ lists.
19
+ * __Modelable__: Provides an `model` helper so we can access the model that the
20
+ controller is for, and a few helpers to access model name.
21
+ * __Messages__: Provide a simple helper for generating message strings from
22
+ I18n. Requires _Modelable_.
23
+ * __Objects__: Provides a simple accessor for `@object` and `@objects`. The
24
+ _ResourceLoaders_ component registers the loaded resources here.
25
+ * __Params__: Provides a way to update the attributes of the object and simple
26
+ starting point for filtering out the permitted resource params. Requires
27
+ _Modelable_, _Objects_.
28
+ * __SaveDestroy__: Provides the `save_object` and `destroy_object` methods.
29
+ Requires _Objects_.
30
+ * __FormHandler__: Provides methods to handle the submit of forms and the
31
+ destroy. Requires _Messages_, _Objects_, _SaveDestroy_. You need to define a
32
+ `location_after_save` and a `location_after_destroy` method, if you want to
33
+ use this module in isolation. The _Resourceful_ component defines these
34
+ methods for you.
35
+ * __ResourceLoaders__: Provides methods for loading the resources. Requires
36
+ _Modelable_ and _Objects_.
37
+ * __ResourcefulUrls__: Generates the URLs to the controller. Requires
38
+ _Modelable_.
39
+ * __Resourceful__: Implements the actual controller actions, registers the
40
+ _before_action_ to load the resources. This is the part that you want to
41
+ include when you want to enjoy the full glory of Tila. Requires _Actionable_,
42
+ _Params_, _FormHandler_, _ResourceLoaders_ and _ResourcefulUrls_.
43
+
44
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Tila'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :tila do
3
+ # # Task goes here
4
+ # end
data/lib/tila.rb ADDED
@@ -0,0 +1,14 @@
1
+ module Tila
2
+ autoload :Actionable, 'tila/actionable'
3
+ autoload :Modelable, 'tila/modelable'
4
+ autoload :Messages, 'tila/messages'
5
+ autoload :Objects, 'tila/objects'
6
+ autoload :Params, 'tila/params'
7
+
8
+ autoload :SaveDestroy, 'tila/save_destroy'
9
+ autoload :FormHandler, 'tila/form_handler'
10
+
11
+ autoload :ResourceLoaders, 'tila/resource_loaders'
12
+ autoload :ResourcefulUrls, 'tila/resourceful_urls'
13
+ autoload :Resourceful, 'tila/resourceful'
14
+ end
@@ -0,0 +1,43 @@
1
+ module Tila
2
+ module Actionable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :action
7
+ end
8
+
9
+ class_methods do
10
+ def collection_actions
11
+ [:index]
12
+ end
13
+
14
+ def new_actions
15
+ [:new, :create]
16
+ end
17
+
18
+ def save_actions
19
+ [:create, :update]
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ def action
26
+ params[:action].to_sym
27
+ end
28
+
29
+ def collection_action?
30
+ self.class.collection_actions.include? action
31
+ end
32
+ def new_action?
33
+ self.class.new_actions.include? action
34
+ end
35
+ def save_action?
36
+ self.class.save_actions.include? action
37
+ end
38
+
39
+ def member_action?
40
+ !collection_action?
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,31 @@
1
+ module Tila
2
+ module FormHandler
3
+ extend ActiveSupport::Concern
4
+ include Messages
5
+ include Objects
6
+ include SaveDestroy
7
+
8
+ protected
9
+
10
+ def handle_form(error_view, message_key)
11
+ respond_to do |format|
12
+ if save_object
13
+ format.html { redirect_to location_after_save, notice: message(message_key) }
14
+ format.json { render :show, status: :created, location: object }
15
+ else
16
+ format.html { render error_view }
17
+ format.json { render json: object.errors, status: :unprocessable_entity }
18
+ end
19
+ end
20
+ end
21
+
22
+ def handle_destroy(message_key)
23
+ destroy_object
24
+
25
+ respond_to do |format|
26
+ format.html { redirect_to location_after_destroy, notice: message(message_key) }
27
+ format.json { head :no_content }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module Tila
2
+ module Messages
3
+ extend ActiveSupport::Concern
4
+ include Modelable
5
+
6
+ protected
7
+
8
+ def message(message_key)
9
+ t(message_key, scope: [:activerecord, :messages, model_name])
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ module Tila
2
+ module Modelable
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def model
7
+ @model ||= controller_path.singularize.camelize.constantize
8
+ end
9
+ end
10
+
11
+ protected
12
+
13
+ def model
14
+ self.class.model
15
+ end
16
+
17
+ def object_name
18
+ self.class.model.model_name.element
19
+ end
20
+ def objects_name
21
+ object_name.pluralize
22
+ end
23
+
24
+ def model_name
25
+ model.model_name.singular
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ module Tila
2
+ module Objects
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attr_accessor :object, :collection
7
+ helper_method :object, :collection
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ module Tila
2
+ module Params
3
+ extend ActiveSupport::Concern
4
+ include Modelable
5
+ include Objects
6
+
7
+ protected
8
+
9
+ def update_resource_params
10
+ object.attributes = permitted_resource_params
11
+ end
12
+
13
+ def permitted_resource_params
14
+ raise "The permitted_resource_params method should be overriden"
15
+ end
16
+
17
+ def resource_params
18
+ params.require(object_name)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,43 @@
1
+ module Tila
2
+ module ResourceLoaders
3
+ extend ActiveSupport::Concern
4
+ include Modelable
5
+ include Objects
6
+
7
+ protected
8
+
9
+ def resource_base
10
+ model
11
+ end
12
+
13
+ def load_resource
14
+ if member_action?
15
+ self.object ||= load_resource_instance
16
+ instance_variable_set("@#{object_name}", object)
17
+ else
18
+ self.collection ||= load_collection
19
+ instance_variable_set("@#{objects_name}", collection)
20
+ end
21
+ end
22
+
23
+ def load_resource_instance
24
+ if new_action?
25
+ build_resource
26
+ elsif params[:id]
27
+ find_resource
28
+ end
29
+ end
30
+
31
+ def find_resource
32
+ resource_base.find(params[:id])
33
+ end
34
+
35
+ def build_resource
36
+ resource_base.new
37
+ end
38
+
39
+ def load_collection
40
+ resource_base.where(nil)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ module Tila
2
+ module Resourceful
3
+ extend ActiveSupport::Concern
4
+ include Actionable
5
+ include Params
6
+ include FormHandler
7
+ include ResourceLoaders
8
+ include ResourcefulUrls
9
+
10
+ included do
11
+ before_action :load_resource
12
+ before_action :update_resource_params, only: save_actions
13
+ end
14
+
15
+ def index; end
16
+ def show; end
17
+ def new; end
18
+ def edit; end
19
+
20
+ def create
21
+ handle_form :new, :created
22
+ end
23
+
24
+ def update
25
+ handle_form :edit, :updated
26
+ end
27
+
28
+ def destroy
29
+ handle_destroy :destroyed
30
+ end
31
+
32
+ protected
33
+
34
+ def location_after_save
35
+ object_url
36
+ end
37
+
38
+ def location_after_destroy
39
+ collection_url
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ module Tila
2
+ module ResourcefulUrls
3
+ extend ActiveSupport::Concern
4
+ include Modelable
5
+
6
+ included do
7
+ helper_method :object_url, :collection_url
8
+ end
9
+
10
+ protected
11
+
12
+ def object_url(object = nil, options = {})
13
+ target = object ? object : self.object
14
+
15
+ send "#{model.model_name.route_key.singularize}_url", target, options
16
+ end
17
+
18
+ def collection_url(options = {})
19
+ send "#{model.model_name.route_key}_url", options
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module Tila
2
+ module SaveDestroy
3
+ extend ActiveSupport::Concern
4
+ include Objects
5
+
6
+ def save_object
7
+ object.save
8
+ end
9
+
10
+ def destroy_object
11
+ object.destroy
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Tila
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.