power_resource 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +46 -0
  6. data/Rakefile +7 -0
  7. data/app/controllers/power_resource/base_controller.rb +30 -0
  8. data/app/helpers/power_resource/base_helper.rb +148 -0
  9. data/app/views/power_resource/base/_actions.html.erb +1 -0
  10. data/app/views/power_resource/base/_collection.html.erb +28 -0
  11. data/app/views/power_resource/base/_form.html.erb +1 -0
  12. data/app/views/power_resource/base/edit.html.erb +3 -0
  13. data/app/views/power_resource/base/index.html.erb +9 -0
  14. data/app/views/power_resource/base/new.html.erb +3 -0
  15. data/app/views/power_resource/builders/_form_for.html.erb +23 -0
  16. data/app/views/power_resource/builders/_formtastic.html.erb +4 -0
  17. data/lib/power_resource.rb +6 -0
  18. data/lib/power_resource/engine.rb +4 -0
  19. data/lib/power_resource/version.rb +3 -0
  20. data/power_resource.gemspec +31 -0
  21. data/spec/dummy/README.rdoc +28 -0
  22. data/spec/dummy/Rakefile +6 -0
  23. data/spec/dummy/app/assets/images/.keep +0 -0
  24. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  25. data/spec/dummy/app/assets/javascripts/posts.js +2 -0
  26. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  27. data/spec/dummy/app/assets/stylesheets/posts.css +4 -0
  28. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  29. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  30. data/spec/dummy/app/controllers/posts_controller.rb +2 -0
  31. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  32. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  33. data/spec/dummy/app/mailers/.keep +0 -0
  34. data/spec/dummy/app/models/.keep +0 -0
  35. data/spec/dummy/app/models/concerns/.keep +0 -0
  36. data/spec/dummy/app/models/post.rb +2 -0
  37. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  38. data/spec/dummy/bin/bundle +3 -0
  39. data/spec/dummy/bin/rails +4 -0
  40. data/spec/dummy/bin/rake +4 -0
  41. data/spec/dummy/config.ru +4 -0
  42. data/spec/dummy/config/application.rb +28 -0
  43. data/spec/dummy/config/boot.rb +5 -0
  44. data/spec/dummy/config/database.yml +25 -0
  45. data/spec/dummy/config/environment.rb +5 -0
  46. data/spec/dummy/config/environments/development.rb +29 -0
  47. data/spec/dummy/config/environments/production.rb +80 -0
  48. data/spec/dummy/config/environments/test.rb +36 -0
  49. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  50. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  51. data/spec/dummy/config/initializers/inflections.rb +16 -0
  52. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  53. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  54. data/spec/dummy/config/initializers/session_store.rb +3 -0
  55. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  56. data/spec/dummy/config/locales/en.yml +23 -0
  57. data/spec/dummy/config/routes.rb +3 -0
  58. data/spec/dummy/db/development.sqlite3 +0 -0
  59. data/spec/dummy/db/migrate/20130722165046_create_posts.rb +10 -0
  60. data/spec/dummy/db/schema.rb +23 -0
  61. data/spec/dummy/db/test.sqlite3 +0 -0
  62. data/spec/dummy/lib/assets/.keep +0 -0
  63. data/spec/dummy/log/.keep +0 -0
  64. data/spec/dummy/log/development.log +1715 -0
  65. data/spec/dummy/log/test.log +2 -0
  66. data/spec/dummy/public/404.html +58 -0
  67. data/spec/dummy/public/422.html +58 -0
  68. data/spec/dummy/public/500.html +57 -0
  69. data/spec/dummy/public/favicon.ico +0 -0
  70. data/spec/dummy/spec/controllers/posts_controller_spec.rb +5 -0
  71. data/spec/dummy/spec/helpers/posts_helper_spec.rb +15 -0
  72. data/spec/dummy/spec/models/post_spec.rb +5 -0
  73. data/spec/helpers/base_helper_spec.rb +8 -0
  74. data/spec/spec_helper.rb +15 -0
  75. metadata +269 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e0f9fc6dee728f9630996271a5beed85771e9cd
4
+ data.tar.gz: d702eaa22dacbb157e5e1308c866a95eec5546fd
5
+ SHA512:
6
+ metadata.gz: fcf0b8d8ec2a666215b8cdc04bf719ffc41bb9e3b4fe383c70a9572c7cc7ed8a76738d21e5463f7bfc785ed3312afbc799cf46ff400c2b0fe977480a51b5835d
7
+ data.tar.gz: 2644d8ca0872750688a129c21f2666f29fd07cdcc1ad2c4eac867e4bb2785befab282d2e7b54eb1931374d664c46c7d82de21a5c576e7fe46aba88b2edb59bbc
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'inherited_resources', github: 'josevalim/inherited_resources'
6
+ gem 'formtastic'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jari Jokinen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # PowerResource
2
+
3
+ Power up your RESTful resources!
4
+
5
+ ALPHA STAGE, DO NOT USE IN PRODUCTION!
6
+
7
+ ## Requirements
8
+
9
+ * Ruby 2.0.0 or greater
10
+ * Rails 4.0.0 or greater
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'power_resource'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install power_resource
25
+
26
+ ## Usage
27
+
28
+ Inherit your resource controllers from PowerResource::BaseController:
29
+
30
+ class ProductsController < PowerResource::BaseController
31
+ end
32
+
33
+ ## Helpers
34
+
35
+ Besides all [helpers from Inherited Resources gem](https://github.com/josevalim/inherited_resources#url-helpers), next helper methods are available:
36
+
37
+ resource_form_path #=> /products when creating a new resource
38
+ #=> /products/1 when resource already exists
39
+
40
+ ## Support
41
+
42
+ If you have any questions or issues with PowerResource, or if you like to report a bug, please create an [issue on GitHub](https://github.com/jarijokinen/power_resource/issues).
43
+
44
+ ## License
45
+
46
+ MIT License. Copyright (c) 2013 [Jari Jokinen](http://jarijokinen.com). See [LICENSE](https://github.com/jarijokinen/power_resource/blob/master/LICENSE.txt) for further details.
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run RSpec'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,30 @@
1
+ module PowerResource
2
+ class BaseController < ::ApplicationController
3
+ inherit_resources
4
+ defaults route_prefix: ''
5
+
6
+ def create
7
+ create! { collection_url }
8
+ end
9
+
10
+ def update
11
+ update! { collection_url }
12
+ end
13
+
14
+ protected
15
+
16
+ def resource_name
17
+ controller_name.tableize.singularize.to_sym
18
+ end
19
+
20
+ def permitted_params
21
+ params.permit(
22
+ resource_name => resource_class.attribute_names - denied_params
23
+ )
24
+ end
25
+
26
+ def denied_params
27
+ %w(id created_at updated_at)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,148 @@
1
+ module PowerResource
2
+ module BaseHelper
3
+ # Returns humanized and localized name for a current resource model
4
+ def resource_human_name
5
+ resource_class.model_name.human
6
+ end
7
+
8
+ # Returns humanized and localized name for a specified resource class
9
+ def resource_human_name_for(resource_class_name)
10
+ eval("#{resource_class_name.to_s.classify}.model_name.human")
11
+ end
12
+
13
+ # Returns an unique title for a resource
14
+ def resource_title
15
+ "#{resource_human_name} #{resource.id}"
16
+ end
17
+
18
+ # Returns a title for a collection
19
+ def collection_title
20
+ I18n.t("activemodel.models.#{controller_name.singularize}.other",
21
+ default: controller_name.humanize)
22
+ end
23
+
24
+ # Returns all attributes for a resource
25
+ def resource_attributes
26
+ resource_class.attribute_names
27
+ end
28
+
29
+ # Returns attributes that should be invisible for end-users
30
+ def non_human_attributes
31
+ %w(id updated_at created_at)
32
+ end
33
+
34
+ # Returns attributes for a resource without non-human attributes
35
+ def resource_human_attributes
36
+ human_attributes = resource_attributes - non_human_attributes
37
+ if respond_to?("parent?")
38
+ parent_attribute = "#{parent.class.name.underscore}_id"
39
+ human_attributes = human_attributes - ["#{parent_attribute}"]
40
+ end
41
+ human_attributes
42
+ end
43
+
44
+ # Returns humanized and localized attribute name
45
+ def attribute_human_name(attribute_name)
46
+ attribute_name = attribute_name.to_s
47
+ I18n.t("activerecord.attributes.#{controller_name.singularize}.#{attribute_name}",
48
+ default: attribute_name.humanize)
49
+ end
50
+
51
+ # Returns truncated attribute value
52
+ def attribute_value(resource, attribute_name, truncation = 50)
53
+ value = resource.send(attribute_name).to_s.truncate(truncation)
54
+ if attribute_name.to_s.match(/_id$/)
55
+ model_name = attribute_name.gsub(/_id$/, "").classify
56
+ begin
57
+ value = eval(model_name).find(value).to_s
58
+ rescue ActiveRecord::RecordNotFound
59
+ value = ""
60
+ end
61
+ end
62
+ value
63
+ end
64
+
65
+ # Returns a text based on action and resource names
66
+ #
67
+ # Example:
68
+ #
69
+ # resource_action(:new)
70
+ # # => "New Product"
71
+ def resource_action(action_name)
72
+ I18n.t("power_resource.resource_actions.#{action_name}",
73
+ resource_name: resource_human_name,
74
+ default: "#{action_name.to_s.titleize} #{resource_human_name}")
75
+ end
76
+
77
+ # Returns a link for a resource
78
+ #
79
+ # Examples:
80
+ #
81
+ # resource_link(:new)
82
+ # # => <a href="/products/new">New Product</a>
83
+ # resource_link(:edit)
84
+ # # => <a href="/products/1/edit">Edit Product</a>
85
+ # resource_link(:edit, row)
86
+ # # => <a href="/products/1/edit">Edit</a>
87
+ # resource_link(:edit, text: "Make changes")
88
+ # # => <a href="/products/1/edit">Make changes</a>
89
+ def resource_link(action_name)
90
+ text ||= resource_action(action_name)
91
+
92
+ eval("resource_link_for_#{action_name.to_s}(text)")
93
+
94
+ case action_name.to_sym
95
+ when :new
96
+ link_to(text, new_resource_path)
97
+ when :edit
98
+ link_to(text, edit_resource_path(resource))
99
+ when :delete
100
+ link_to
101
+ end
102
+ end
103
+
104
+ # Returns form path for a resource
105
+ def resource_form_path
106
+ resource.new_record? ? collection_path : resource_path
107
+ end
108
+
109
+ # Renders form using selected form builder
110
+ def render_form(form_builder = "formtastic")
111
+ fields = resource_human_attributes
112
+ fields.map! do |arg|
113
+ arg.to_s.sub("_id", "").to_sym
114
+ end
115
+ render "power_resource/builders/#{form_builder}", fields: fields
116
+ end
117
+
118
+ # Renders collection table
119
+ def render_collection_table(custom_attributes = nil)
120
+ render "collection",
121
+ collection: collection,
122
+ attributes: custom_attributes || resource_human_attributes
123
+ end
124
+
125
+ # Renders action links for a resource
126
+ def render_actions_for(resource)
127
+ render "actions", resource: resource
128
+ end
129
+
130
+ private
131
+
132
+ def resource_link_for_index(text)
133
+ link_to(text, collection_path)
134
+ end
135
+
136
+ def resource_link_for_show(text)
137
+ link_to(text, resource_path(resource))
138
+ end
139
+
140
+ def resource_link_for_new(text)
141
+ link_to(text, new_resource_path)
142
+ end
143
+
144
+ def resource_link_for_edit(text)
145
+ link_to(text, edit_resource_path(resource))
146
+ end
147
+ end
148
+ end
@@ -0,0 +1 @@
1
+ <%= link_to(t('actions.edit'), edit_resource_path(resource)) %>
@@ -0,0 +1,28 @@
1
+ <% @title ||= collection_title %>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <% attributes.each do |attribute_name| %>
7
+ <th><%= attribute_human_name(attribute_name) %></th>
8
+ <% end %>
9
+ <th>&nbsp;</th>
10
+ </tr>
11
+ </thead>
12
+ <tbody>
13
+ <% collection.each do |resource| %>
14
+ <tr>
15
+ <% attributes.each do |attribute_name| %>
16
+ <td><%= attribute_value(resource, attribute_name) %></td>
17
+ <% end %>
18
+ <td class="actions"><%= render_actions_for(resource) %></td>
19
+ </tr>
20
+ <% end %>
21
+ </tbody>
22
+ </table>
23
+
24
+ <%=
25
+ if defined?(Kaminari)
26
+ paginate(collection)
27
+ end
28
+ %>
@@ -0,0 +1 @@
1
+ <%= render_form %>
@@ -0,0 +1,3 @@
1
+ <% @title ||= resource_action(:edit) %>
2
+
3
+ <%= render 'form' %>
@@ -0,0 +1,9 @@
1
+ <% @title ||= collection_title %>
2
+
3
+ <%=
4
+ unless collection.blank?
5
+ render_collection_table
6
+ end
7
+ %>
8
+
9
+ <%= resource_link(:new) %>
@@ -0,0 +1,3 @@
1
+ <% @title ||= resource_action(:new) %>
2
+
3
+ <%= render 'form' %>
@@ -0,0 +1,23 @@
1
+ <% if resource.errors.any? %>
2
+ <div id="error_explanation">
3
+ <%= pluralize(resource.errors.count, "error") %>
4
+ prohibited this resource from being saved:
5
+ <ul>
6
+ <% resource.errors.full_messages.each do |msg| %>
7
+ <li><%= msg %></li>
8
+ <% end %>
9
+ </ul>
10
+ </div>
11
+ <% end %>
12
+
13
+ <%= form_for(resource, url: resource_form_path) do |f| %>
14
+ <ul>
15
+ <% fields.each do |field| %>
16
+ <li>
17
+ <%= f.label(field) %>
18
+ <%= f.text_field(field) %>
19
+ </li>
20
+ <% end %>
21
+ </ul>
22
+ <%= f.submit %>
23
+ <% end %>
@@ -0,0 +1,4 @@
1
+ <%= semantic_form_for(resource, url: resource_form_path) do |f| %>
2
+ <%= f.inputs(*fields) %>
3
+ <%= f.actions %>
4
+ <% end %>
@@ -0,0 +1,6 @@
1
+ require 'inherited_resources'
2
+ require 'power_resource/engine'
3
+ require 'power_resource/version'
4
+
5
+ module PowerResource
6
+ end
@@ -0,0 +1,4 @@
1
+ module PowerResource
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module PowerResource
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'power_resource/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'power_resource'
8
+ spec.version = PowerResource::VERSION
9
+ spec.authors = ['Jari Jokinen']
10
+ spec.email = ['info@jarijokinen.com']
11
+ spec.description = 'Power up your RESTful resources!'
12
+ spec.summary = 'Power up your RESTful resources!'
13
+ spec.homepage = 'https://github.com/jarijokinen/power_resource'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^spec/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.platform = Gem::Platform::RUBY
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_dependency 'rails', '~> 4.0.0'
24
+ spec.add_dependency 'inherited_resources', '~> 1.4.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.3'
27
+ spec.add_development_dependency 'capybara', '2.1.0'
28
+ spec.add_development_dependency 'rake', '10.1.0'
29
+ spec.add_development_dependency 'rspec-rails', '2.14.0'
30
+ spec.add_development_dependency 'sqlite3', '1.3.7'
31
+ 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>.