power_resource 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -2
  3. data/README.md +77 -10
  4. data/app/controllers/power_resource/base_controller.rb +19 -7
  5. data/app/helpers/power_resource/action_helper.rb +147 -0
  6. data/app/helpers/power_resource/base_helper.rb +51 -103
  7. data/app/helpers/power_resource/collection_helper.rb +20 -0
  8. data/app/helpers/power_resource/configuration_helper.rb +11 -0
  9. data/app/helpers/power_resource/rendering_helper.rb +26 -0
  10. data/app/views/power_resource/base/_actions.html.erb +3 -1
  11. data/app/views/power_resource/base/_collection.html.erb +5 -11
  12. data/app/views/power_resource/base/edit.html.erb +1 -1
  13. data/app/views/power_resource/base/index.html.erb +1 -1
  14. data/app/views/power_resource/base/new.html.erb +1 -1
  15. data/app/views/power_resource/builders/_form_for.html.erb +12 -3
  16. data/lib/power_resource.rb +1 -0
  17. data/lib/power_resource/configuration.rb +19 -0
  18. data/lib/power_resource/version.rb +1 -1
  19. data/power_resource.gemspec +9 -7
  20. data/spec/dummy/app/assets/javascripts/application.js +2 -0
  21. data/spec/dummy/app/assets/javascripts/categories.js +2 -0
  22. data/spec/dummy/app/assets/javascripts/comments.js +2 -0
  23. data/spec/dummy/app/assets/stylesheets/categories.css +4 -0
  24. data/spec/dummy/app/assets/stylesheets/comments.css +4 -0
  25. data/spec/dummy/app/controllers/backend/categories_controller.rb +7 -0
  26. data/spec/dummy/app/controllers/backend/comments_controller.rb +9 -0
  27. data/spec/dummy/app/controllers/backend/posts_controller.rb +7 -0
  28. data/spec/dummy/app/controllers/categories_controller.rb +5 -0
  29. data/spec/dummy/app/controllers/comments_controller.rb +7 -0
  30. data/spec/dummy/app/controllers/posts_controller.rb +3 -0
  31. data/spec/dummy/app/helpers/categories_helper.rb +2 -0
  32. data/spec/dummy/app/helpers/comments_helper.rb +2 -0
  33. data/spec/dummy/app/models/category.rb +5 -0
  34. data/spec/dummy/app/models/comment.rb +3 -0
  35. data/spec/dummy/app/models/post.rb +2 -0
  36. data/spec/dummy/app/views/layouts/application.html.erb +23 -11
  37. data/spec/dummy/config/initializers/secret_token.rb +1 -1
  38. data/spec/dummy/config/locales/en.yml +11 -22
  39. data/spec/dummy/config/locales/fi.yml +38 -0
  40. data/spec/dummy/config/routes.rb +13 -1
  41. data/spec/dummy/db/development.sqlite3 +0 -0
  42. data/spec/dummy/db/migrate/{20130722165046_create_posts.rb → 20130915095616_create_posts.rb} +1 -0
  43. data/spec/dummy/db/migrate/20130916174608_create_categories.rb +9 -0
  44. data/spec/dummy/db/migrate/20130916180755_create_comments.rb +11 -0
  45. data/spec/dummy/db/schema.rb +18 -1
  46. data/spec/dummy/db/test.sqlite3 +0 -0
  47. data/spec/dummy/log/development.log +17445 -674
  48. data/spec/dummy/log/test.log +20826 -2
  49. data/spec/features/posts_spec.rb +8 -0
  50. data/spec/helpers/action_helper_spec.rb +111 -0
  51. data/spec/helpers/base_helper_spec.rb +111 -1
  52. data/spec/helpers/collection_helper_spec.rb +53 -0
  53. data/spec/helpers/rendering_helper_spec.rb +162 -0
  54. data/spec/spec_helper.rb +11 -0
  55. metadata +92 -26
  56. data/app/views/power_resource/builders/_formtastic.html.erb +0 -4
  57. data/spec/dummy/spec/controllers/posts_controller_spec.rb +0 -5
  58. data/spec/dummy/spec/helpers/posts_helper_spec.rb +0 -15
  59. data/spec/dummy/spec/models/post_spec.rb +0 -5
@@ -0,0 +1,20 @@
1
+ module PowerResource
2
+ module CollectionHelper
3
+ # Returns a name for a current collection
4
+ def collection_name
5
+ resource_name.tableize
6
+ end
7
+
8
+ # Returns humanized and localized name for a current collection
9
+ def collection_human_name
10
+ I18n.t("activerecord.models.#{resource_name}.other",
11
+ default: collection_name.humanize)
12
+ end
13
+
14
+ # Returns a title for a current collection
15
+ def collection_title(options = {})
16
+ I18n.t("power_resource.titles.#{resource_name}.collection",
17
+ { default: "#{collection_human_name}" }.merge(options) )
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module PowerResource
2
+ module ConfigurationHelper
3
+ def collection_table_classes
4
+ PowerResource::Configuration.collection_table_classes.join(' ')
5
+ end
6
+
7
+ def collection_table_button_classes
8
+ PowerResource::Configuration.collection_table_button_classes.join(' ')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ module PowerResource
2
+ module RenderingHelper
3
+ # Renders collection table
4
+ def render_collection_table(custom_attributes = nil)
5
+ render 'collection',
6
+ collection: collection,
7
+ attributes: custom_attributes || resource_human_attributes,
8
+ collection_table_classes: collection_table_classes,
9
+ collection_table_button_classes: collection_table_button_classes
10
+ end
11
+
12
+ # Renders form using selected form builder
13
+ def render_form(form_builder = 'form_for')
14
+ fields = resource_human_attributes
15
+ fields.map! do |arg|
16
+ arg.to_s.sub('_id', '').to_sym
17
+ end
18
+ render "power_resource/builders/#{form_builder}", fields: fields
19
+ end
20
+
21
+ # Renders actions for a specific resource
22
+ def render_actions_for(resource)
23
+ render 'actions', resource: resource
24
+ end
25
+ end
26
+ end
@@ -1 +1,3 @@
1
- <%= link_to(t('actions.edit'), edit_resource_path(resource)) %>
1
+ <%= resource_association_links_for(resource) %>
2
+ <%= resource_link_to(:edit, resource) %>
3
+ <%= resource_link_to(:delete, resource) %>
@@ -1,28 +1,22 @@
1
1
  <% @title ||= collection_title %>
2
2
 
3
- <table>
3
+ <table class="<%= collection_table_classes %>">
4
4
  <thead>
5
5
  <tr>
6
6
  <% attributes.each do |attribute_name| %>
7
- <th><%= attribute_human_name(attribute_name) %></th>
7
+ <th><%= resource_attribute_human_name_for(attribute_name) %></th>
8
8
  <% end %>
9
9
  <th>&nbsp;</th>
10
10
  </tr>
11
11
  </thead>
12
12
  <tbody>
13
- <% collection.each do |resource| %>
13
+ <% collection.each do |row| %>
14
14
  <tr>
15
15
  <% attributes.each do |attribute_name| %>
16
- <td><%= attribute_value(resource, attribute_name) %></td>
16
+ <td><%= attribute_value_for(row, attribute_name) %></td>
17
17
  <% end %>
18
- <td class="actions"><%= render_actions_for(resource) %></td>
18
+ <td><%= render_actions_for(row) %></td>
19
19
  </tr>
20
20
  <% end %>
21
21
  </tbody>
22
22
  </table>
23
-
24
- <%=
25
- if defined?(Kaminari)
26
- paginate(collection)
27
- end
28
- %>
@@ -1,3 +1,3 @@
1
- <% @title ||= resource_action(:edit) %>
1
+ <% @title ||= resource_action_title(:edit) %>
2
2
 
3
3
  <%= render 'form' %>
@@ -6,4 +6,4 @@
6
6
  end
7
7
  %>
8
8
 
9
- <%= resource_link(:new) %>
9
+ <%= resource_link_to(:new) %>
@@ -1,3 +1,3 @@
1
- <% @title ||= resource_action(:new) %>
1
+ <% @title ||= resource_action_title(:new) %>
2
2
 
3
3
  <%= render 'form' %>
@@ -1,6 +1,6 @@
1
1
  <% if resource.errors.any? %>
2
2
  <div id="error_explanation">
3
- <%= pluralize(resource.errors.count, "error") %>
3
+ <%= pluralize(resource.errors.count, 'error') %>
4
4
  prohibited this resource from being saved:
5
5
  <ul>
6
6
  <% resource.errors.full_messages.each do |msg| %>
@@ -12,10 +12,19 @@
12
12
 
13
13
  <%= form_for(resource, url: resource_form_path) do |f| %>
14
14
  <ul>
15
- <% fields.each do |field| %>
15
+ <% resource_human_attributes.each do |field| %>
16
16
  <li>
17
17
  <%= f.label(field) %>
18
- <%= f.text_field(field) %>
18
+ <%=
19
+ if resource_class.columns_hash["#{field}"].type == :text
20
+ f.text_area(field)
21
+ elsif field.match(/_id$/)
22
+ parent_class = field.sub(/_id$/, '').classify
23
+ f.select(field, eval(parent_class).all.map { |p| [p.name, p.id] })
24
+ else
25
+ f.text_field(field)
26
+ end
27
+ %>
19
28
  </li>
20
29
  <% end %>
21
30
  </ul>
@@ -1,4 +1,5 @@
1
1
  require 'inherited_resources'
2
+ require 'power_resource/configuration'
2
3
  require 'power_resource/engine'
3
4
  require 'power_resource/version'
4
5
 
@@ -0,0 +1,19 @@
1
+ module PowerResource
2
+ module Configuration
3
+ # Classes for collection tables
4
+ #
5
+ # Default values
6
+ #
7
+ # collection_table_classes = %w(table)
8
+ mattr_accessor :collection_table_classes
9
+ self.collection_table_classes = %w(table)
10
+
11
+ # Classes for collection table buttons (edit, delete, etc.)
12
+ #
13
+ # Default values
14
+ #
15
+ # collection_table_button_classes = %w(btn btn-default btn-xs)
16
+ mattr_accessor :collection_table_button_classes
17
+ self.collection_table_button_classes = %w(btn btn-default btn-xs)
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module PowerResource
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = PowerResource::VERSION
9
9
  spec.authors = ['Jari Jokinen']
10
10
  spec.email = ['info@jarijokinen.com']
11
- spec.description = 'Power up your RESTful resources!'
12
- spec.summary = 'Power up your RESTful resources!'
11
+ spec.description = 'Power up RESTful resources!'
12
+ spec.summary = 'Power up RESTful resources!'
13
13
  spec.homepage = 'https://github.com/jarijokinen/power_resource'
14
14
  spec.license = 'MIT'
15
15
 
@@ -21,11 +21,13 @@ Gem::Specification.new do |spec|
21
21
  spec.required_ruby_version = '>= 2.0.0'
22
22
 
23
23
  spec.add_dependency 'rails', '~> 4.0.0'
24
- spec.add_dependency 'inherited_resources', '~> 1.4.0'
24
+ spec.add_dependency 'activerecord', '~> 4.0.0'
25
+ spec.add_dependency 'inherited_resources', '~> 1.4.1'
25
26
 
26
27
  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'
28
+ spec.add_development_dependency 'capybara', '~> 2.1.0'
29
+ spec.add_development_dependency 'database_cleaner', '~> 1.0.1'
30
+ spec.add_development_dependency 'rake', '~> 10.1.0'
31
+ spec.add_development_dependency 'rspec-rails', '~> 2.14.0'
32
+ spec.add_development_dependency 'sqlite3', '~> 1.3.8'
31
33
  end
@@ -10,4 +10,6 @@
10
10
  // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
11
  // about supported directives.
12
12
  //
13
+ //= require jquery
14
+ //= require jquery_ujs
13
15
  //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,7 @@
1
+ module Backend
2
+ class CategoriesController < PowerResource::BaseController
3
+ def permit_attributes
4
+ %w(name)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Backend
2
+ class CommentsController < PowerResource::BaseController
3
+ belongs_to :post
4
+
5
+ def permit_attributes
6
+ %w(content author)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module Backend
2
+ class PostsController < PowerResource::BaseController
3
+ def permit_attributes
4
+ %w(category_id title content)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class CategoriesController < PowerResource::BaseController
2
+ def permit_attributes
3
+ %w(name)
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class CommentsController < PowerResource::BaseController
2
+ belongs_to :post
3
+
4
+ def permit_attributes
5
+ %w(content author)
6
+ end
7
+ end
@@ -1,2 +1,5 @@
1
1
  class PostsController < PowerResource::BaseController
2
+ def permit_attributes
3
+ %w(category_id title content)
4
+ end
2
5
  end
@@ -0,0 +1,2 @@
1
+ module CategoriesHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module CommentsHelper
2
+ end
@@ -0,0 +1,5 @@
1
+ class Category < ActiveRecord::Base
2
+ def to_s
3
+ name
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ class Comment < ActiveRecord::Base
2
+ belongs_to :post
3
+ end
@@ -1,2 +1,4 @@
1
1
  class Post < ActiveRecord::Base
2
+ belongs_to :category
3
+ has_many :comments
2
4
  end
@@ -1,14 +1,26 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
- <head>
4
- <title>Dummy</title>
5
- <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
- <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
- <%= csrf_meta_tags %>
8
- </head>
9
- <body>
10
-
11
- <%= yield %>
12
-
13
- </body>
3
+ <head>
4
+ <title><%= "#{@title} | " unless @title.blank? %>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <nav>
11
+ <ul>
12
+ <li><%= link_to('Posts', posts_path) %></li>
13
+ <li><%= link_to('Categories', categories_path) %></li>
14
+ </ul>
15
+ Backend:
16
+ <ul>
17
+ <li><%= link_to('Posts', backend_posts_path) %></li>
18
+ <li><%= link_to('Categories', backend_categories_path) %></li>
19
+ </ul>
20
+ </nav>
21
+ <section>
22
+ <%= content_tag(:h1, @title) unless @title.blank? %>
23
+ <%= yield %>
24
+ </section>
25
+ </body>
14
26
  </html>
@@ -9,4 +9,4 @@
9
9
 
10
10
  # Make sure your secret_key_base is kept private
11
11
  # if you're sharing your code publicly.
12
- Dummy::Application.config.secret_key_base = 'f1926c442ab84f237a21d9c4039f58d30a3c2944f6d1fc001c068498f34bb0c41ae75eca819ecf4c156ea4e1690374256bbce1e77a86ae1db56e0f36880d62a8'
12
+ Dummy::Application.config.secret_key_base = 'f2a0cd4e95e90681626669aa7832968a9c6c298583fb0264c115015f34fcc7fac21546015d5bd1d0cbbe9b17ece4fbfdcc3de617d169b0333ab9d667ba67302c'
@@ -1,23 +1,12 @@
1
- # Files in the config/locales directory are used for internationalization
2
- # and are automatically loaded by Rails. If you want to use locales other
3
- # than English, add the necessary files in this directory.
4
- #
5
- # To use the locales, use `I18n.t`:
6
- #
7
- # I18n.t 'hello'
8
- #
9
- # In views, this is aliased to just `t`:
10
- #
11
- # <%= t('hello') %>
12
- #
13
- # To use a different locale, set it with `I18n.locale`:
14
- #
15
- # I18n.locale = :es
16
- #
17
- # This would use the information in config/locales/es.yml.
18
- #
19
- # To learn more, please read the Rails Internationalization guide
20
- # available at http://guides.rubyonrails.org/i18n.html.
21
-
22
1
  en:
23
- hello: "Hello world"
2
+ activerecord:
3
+ models:
4
+ category:
5
+ one: 'Category'
6
+ other: 'Categories'
7
+ comment:
8
+ one: 'Comment'
9
+ other: 'Comments'
10
+ post:
11
+ one: 'Post'
12
+ other: 'Posts'
@@ -0,0 +1,38 @@
1
+ fi:
2
+ activerecord:
3
+ attributes:
4
+ post:
5
+ category_id: 'Kategoria'
6
+ title: 'Otsikko'
7
+ content: 'Sisältö'
8
+ models:
9
+ category:
10
+ one: 'Kategoria'
11
+ other: 'Kategoriat'
12
+ comment:
13
+ one: 'Kommentti'
14
+ other: 'Kommentit'
15
+ post:
16
+ one: 'Artikkeli'
17
+ other: 'Artikkelit'
18
+ power_resource:
19
+ actions:
20
+ new: 'Uusi'
21
+ edit: 'Muokkaa'
22
+ delete: 'Poista'
23
+ duplicate: 'Kopioi'
24
+ resource_actions:
25
+ new: 'Uusi %{downcased_resource_name}'
26
+ edit: 'Muokkaa %{resource_name}'
27
+ delete: 'Poista %{resource_name}'
28
+ duplicate: 'Kopioi %{resource_name}'
29
+ links:
30
+ post:
31
+ edit: 'Muokkaa'
32
+ titles:
33
+ post:
34
+ resource: 'Artikkeli nro %{resource_id}'
35
+ collection: 'Lista artikkeleista'
36
+ edit: 'Muokkaa artikkelia'
37
+ confirmations:
38
+ delete: 'Oletko varma?'
@@ -1,3 +1,15 @@
1
1
  Dummy::Application.routes.draw do
2
- resources :posts
2
+ namespace :backend, path: 'admin' do
3
+ resources :posts do
4
+ resources :comments
5
+ end
6
+ resources :categories
7
+ root to: 'posts#index'
8
+ end
9
+
10
+ resources :categories
11
+ resources :posts do
12
+ resources :comments
13
+ end
14
+ root to: 'posts#index'
3
15
  end