simple_resource 0.0.1 → 0.1.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.
Files changed (42) hide show
  1. data/.travis.yml +10 -0
  2. data/CHANGELOG.md +9 -0
  3. data/{README_FOR_DEVELOPERS.md → DEVELOPER_GUIDE.md} +0 -0
  4. data/Gemfile +0 -1
  5. data/Guardfile +1 -1
  6. data/README.md +2 -0
  7. data/Rakefile +6 -0
  8. data/app/helpers/simple_resource/base_helper.rb +115 -0
  9. data/app/views/simple_resource/base/_actions.html.erb +1 -0
  10. data/app/views/simple_resource/base/_collection.html.erb +22 -0
  11. data/app/views/simple_resource/base/_form.html.erb +1 -0
  12. data/app/views/simple_resource/base/edit.html.erb +3 -0
  13. data/app/views/simple_resource/base/index.html.erb +4 -0
  14. data/app/views/simple_resource/base/new.html.erb +3 -0
  15. data/app/views/simple_resource/base/show.html.erb +12 -0
  16. data/app/views/simple_resource/builders/_formtastic.html.erb +7 -0
  17. data/lib/simple_resource/version.rb +1 -1
  18. data/spec/dummy/app/controllers/backend/languages_controller.rb +2 -0
  19. data/spec/dummy/app/controllers/categories_controller.rb +2 -0
  20. data/spec/dummy/app/controllers/languages_controller.rb +2 -0
  21. data/spec/dummy/app/controllers/posts_controller.rb +3 -0
  22. data/spec/dummy/app/models/category.rb +4 -0
  23. data/spec/dummy/app/models/language.rb +4 -0
  24. data/spec/dummy/app/models/post.rb +5 -0
  25. data/spec/dummy/app/views/layouts/application.html.erb +10 -11
  26. data/spec/dummy/config/environments/test.rb +1 -1
  27. data/spec/dummy/config/locales/fi.yml +16 -0
  28. data/spec/dummy/config/routes.rb +10 -53
  29. data/spec/dummy/db/migrate/20120920095910_create_languages.rb +9 -0
  30. data/spec/dummy/db/migrate/20120920161043_create_categories.rb +10 -0
  31. data/spec/dummy/db/migrate/20120920161127_create_posts.rb +14 -0
  32. data/spec/dummy/db/schema.rb +41 -0
  33. data/spec/factories/categories.rb +6 -0
  34. data/spec/factories/languages.rb +5 -0
  35. data/spec/factories/posts.rb +8 -0
  36. data/spec/helpers/base_helper_spec.rb +438 -0
  37. data/spec/integration/languages_spec.rb +39 -0
  38. data/spec/spec_helper.rb +7 -5
  39. data/spec/support/helpers.rb +9 -0
  40. metadata +41 -7
  41. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  42. data/spec/dummy/config/locales/en.yml +0 -5
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ before_script:
6
+ - "cd spec/dummy; rake db:migrate; cd ../.."
7
+ - "cd spec/dummy; rake db:test:prepare; cd ../.."
8
+ notifications:
9
+ recipients:
10
+ - info@jarijokinen.com
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ 0.1.0 (October 19, 2012)
4
+
5
+ * First release with basic functionality
6
+
7
+ 0.0.1 (September 20, 2012)
8
+
9
+ * Initial release; no functionality yet
File without changes
data/Gemfile CHANGED
@@ -2,7 +2,6 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem "factory_girl_rails"
6
5
  gem "jquery-rails"
7
6
  gem "formtastic"
8
7
  gem "simple_form"
data/Guardfile CHANGED
@@ -8,7 +8,7 @@ guard "spork", rspec_env: { "RAILS_ENV" => "test" } do
8
8
  watch("spec/spec_helper.rb") { :rspec }
9
9
  end
10
10
 
11
- guard "rspec", cli: "--drb --color", version: 2, all_on_start: false, all_after_pass: false do
11
+ guard "rspec", cli: "--drb --color", all_on_start: false, all_after_pass: false do
12
12
  watch(%r{^spec/.+_spec\.rb$})
13
13
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
14
14
  watch("spec/spec_helper.rb") { "spec" }
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # SimpleResource
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/jarijokinen/simple_resource.png)](http://travis-ci.org/jarijokinen/simple_resource) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/jarijokinen/simple_resource)
4
+
3
5
  SimpleResource speeds up development of standard Rails applications by integrating [Inherited Resources](https://github.com/josevalim/inherited_resources), inherited views and form builders together.
4
6
 
5
7
  ## Features
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
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
@@ -1,4 +1,119 @@
1
1
  module SimpleResource
2
2
  module BaseHelper
3
+ def resource_human_name(resource_class_name = nil)
4
+ if resource_class_name
5
+ eval("#{resource_class_name}.model_name.human")
6
+ else
7
+ resource_class.model_name.human
8
+ end
9
+ end
10
+
11
+ def resource_title
12
+ "#{resource_human_name} #{resource.id}"
13
+ end
14
+
15
+ def collection_title
16
+ I18n.t("activerecord.models.#{controller_name.singularize}.other",
17
+ default: controller_name.humanize)
18
+ end
19
+
20
+ def new_resource_title
21
+ I18n.t("simple_resource.new_resource", resource_name: resource_human_name,
22
+ default: "New #{resource_human_name}")
23
+ end
24
+
25
+ def new_resource_link
26
+ link_to(new_resource_title, new_resource_path)
27
+ end
28
+
29
+ def edit_resource_title
30
+ I18n.t("simple_resource.edit_resource", resource_name: resource_human_name,
31
+ default: "Edit #{resource_human_name}")
32
+ end
33
+
34
+ def resource_attributes
35
+ resource_class.attribute_names
36
+ end
37
+
38
+ def non_human_attributes
39
+ %w(id updated_at created_at)
40
+ end
41
+
42
+ def resource_human_attributes
43
+ human_attributes = resource_attributes - non_human_attributes
44
+ if respond_to?("parent?")
45
+ human_attributes = human_attributes - ["#{parent.class.name.underscore}_id"]
46
+ end
47
+ human_attributes
48
+ end
49
+
50
+ def attribute_human_name(attribute_name)
51
+ attribute_name = attribute_name.to_s
52
+ I18n.t("activerecord.attributes.#{controller_name.singularize}.#{attribute_name}",
53
+ default: attribute_name.humanize)
54
+ end
55
+
56
+ def attribute_value(resource, attribute_name, truncation = 50)
57
+ value = resource.send(attribute_name).to_s.truncate(truncation)
58
+ if attribute_name.to_s.match(/_id$/)
59
+ model_name = attribute_name.gsub(/_id$/, "").classify
60
+ value = eval(model_name).find(value).to_s
61
+ end
62
+ value
63
+ end
64
+
65
+ def link_to_action(action_name, title, path)
66
+ action_name = action_name.to_sym
67
+ if action_name == :delete
68
+ link_to(t("simple_resource.#{action_name.to_s}", default: title), path,
69
+ method: :delete, confirm: t("simple_resource.delete_confirmation", default: "Are you sure?"))
70
+ else
71
+ link_to(t("simple_resource.#{action_name.to_s}", default: title), path)
72
+ end
73
+ end
74
+
75
+ def default_actions_for(resource)
76
+ html = Array.new
77
+ html << link_to_action(:show, "Show", resource_path(resource))
78
+ html << link_to_action(:edit, "Edit", edit_resource_path(resource))
79
+ html << link_to_action(:delete, "Delete", resource_path(resource))
80
+ html.join("\n").html_safe
81
+ end
82
+
83
+ def controller_namespaces
84
+ namespaces = controller_path.split("/")
85
+ namespaces.pop
86
+ namespaces
87
+ end
88
+
89
+ def resource_form_path
90
+ if controller_namespaces.empty?
91
+ if !resource.new_record?
92
+ resource_path
93
+ else
94
+ collection_path
95
+ end
96
+ else
97
+ controller_namespaces | [resource]
98
+ end
99
+ end
100
+
101
+ def render_actions_for(resource)
102
+ render "actions", resource: resource
103
+ end
104
+
105
+ def render_collection_table(custom_attributes = nil)
106
+ render "collection",
107
+ collection: collection,
108
+ attributes: custom_attributes || resource_human_attributes
109
+ end
110
+
111
+ def render_form(form_builder = "formtastic")
112
+ fields = resource_human_attributes
113
+ fields.map! do |arg|
114
+ arg.to_s.sub("_id", "").to_sym
115
+ end
116
+ render "simple_resource/builders/#{form_builder}", fields: fields
117
+ end
3
118
  end
4
119
  end
@@ -0,0 +1 @@
1
+ <%= default_actions_for resource %>
@@ -0,0 +1,22 @@
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>
@@ -0,0 +1 @@
1
+ <%= render_form "formtastic" %>
@@ -0,0 +1,3 @@
1
+ <% @title ||= edit_resource_title %>
2
+
3
+ <%= render "form" %>
@@ -0,0 +1,4 @@
1
+ <% @title ||= collection_title %>
2
+
3
+ <%= render_collection_table %>
4
+ <%= new_resource_link %>
@@ -0,0 +1,3 @@
1
+ <% @title ||= new_resource_title %>
2
+
3
+ <%= render "form" %>
@@ -0,0 +1,12 @@
1
+ <% @title ||= resource_title %>
2
+
3
+ <table>
4
+ <tbody>
5
+ <% resource_attributes.each do |attribute_name| %>
6
+ <tr>
7
+ <th><%= attribute_human_name(attribute_name) %></th>
8
+ <td><%= attribute_value(resource, attribute_name) %></td>
9
+ </tr>
10
+ <% end %>
11
+ </tbody>
12
+ </table>
@@ -0,0 +1,7 @@
1
+ <%= semantic_form_for resource, url: resource_form_path do |f| %>
2
+ <%= f.inputs(*(fields)) %>
3
+ <%= f.actions do %>
4
+ <%= f.action :submit %>
5
+ <%= f.action :cancel, label: t(:cancel, default: "Cancel"), url: collection_path %>
6
+ <% end %>
7
+ <% end %>
@@ -1,3 +1,3 @@
1
1
  module SimpleResource
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,2 @@
1
+ class Backend::LanguagesController < SimpleResource::BaseController
2
+ end
@@ -0,0 +1,2 @@
1
+ class CategoriesController < SimpleResource::BaseController
2
+ end
@@ -0,0 +1,2 @@
1
+ class LanguagesController < SimpleResource::BaseController
2
+ end
@@ -0,0 +1,3 @@
1
+ class PostsController < SimpleResource::BaseController
2
+ belongs_to :category
3
+ end
@@ -0,0 +1,4 @@
1
+ class Category < ActiveRecord::Base
2
+ has_many :posts
3
+ attr_accessible :description, :name
4
+ end
@@ -0,0 +1,4 @@
1
+ class Language < ActiveRecord::Base
2
+ attr_accessible :name
3
+ has_many :posts
4
+ end
@@ -0,0 +1,5 @@
1
+ class Post < ActiveRecord::Base
2
+ belongs_to :category
3
+ belongs_to :language
4
+ attr_accessible :content, :language_id, :title
5
+ end
@@ -1,14 +1,13 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
- <head>
4
- <title>Dummy</title>
5
- <%= stylesheet_link_tag "application", :media => "all" %>
6
- <%= javascript_include_tag "application" %>
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" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <%= content_tag(:h1, @title) unless @title.blank? %>
11
+ <%= yield %>
12
+ </body>
14
13
  </html>
@@ -5,7 +5,7 @@ Dummy::Application.configure do
5
5
  # test suite. You never need to work with it otherwise. Remember that
6
6
  # your test database is "scratch space" for the test suite and is wiped
7
7
  # and recreated between test runs. Don't rely on the data there!
8
- config.cache_classes = true
8
+ config.cache_classes = !(ENV['DRB'] == 'true')
9
9
 
10
10
  # Configure static asset server for tests with Cache-Control for performance
11
11
  config.serve_static_assets = true
@@ -0,0 +1,16 @@
1
+ fi:
2
+ activerecord:
3
+ models:
4
+ language:
5
+ one: "Kieli"
6
+ other: "Kielet"
7
+ attributes:
8
+ language:
9
+ name: "Nimi"
10
+ simple_resource:
11
+ new_resource: "Uusi %{resource_name}"
12
+ edit_resource: "Muokkaa %{resource_name}"
13
+ show: "Näytä"
14
+ edit: "Muokkaa"
15
+ delete: "Poista"
16
+ delete_confirmation: "Oletko varma?"
@@ -1,58 +1,15 @@
1
1
  Dummy::Application.routes.draw do
2
- # The priority is based upon order of creation:
3
- # first created -> highest priority.
2
+ namespace :backend do
3
+ resources :categories do
4
+ resources :posts
5
+ end
4
6
 
5
- # Sample of regular route:
6
- # match 'products/:id' => 'catalog#view'
7
- # Keep in mind you can assign values other than :controller and :action
7
+ resources :languages
8
+ end
8
9
 
9
- # Sample of named route:
10
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
11
- # This route can be invoked with purchase_url(:id => product.id)
10
+ resources :categories do
11
+ resources :posts
12
+ end
12
13
 
13
- # Sample resource route (maps HTTP verbs to controller actions automatically):
14
- # resources :products
15
-
16
- # Sample resource route with options:
17
- # resources :products do
18
- # member do
19
- # get 'short'
20
- # post 'toggle'
21
- # end
22
- #
23
- # collection do
24
- # get 'sold'
25
- # end
26
- # end
27
-
28
- # Sample resource route with sub-resources:
29
- # resources :products do
30
- # resources :comments, :sales
31
- # resource :seller
32
- # end
33
-
34
- # Sample resource route with more complex sub-resources
35
- # resources :products do
36
- # resources :comments
37
- # resources :sales do
38
- # get 'recent', :on => :collection
39
- # end
40
- # end
41
-
42
- # Sample resource route within a namespace:
43
- # namespace :admin do
44
- # # Directs /admin/products/* to Admin::ProductsController
45
- # # (app/controllers/admin/products_controller.rb)
46
- # resources :products
47
- # end
48
-
49
- # You can have the root of your site routed with "root"
50
- # just remember to delete public/index.html.
51
- # root :to => 'welcome#index'
52
-
53
- # See how all your routes lay out with "rake routes"
54
-
55
- # This is a legacy wild controller route that's not recommended for RESTful applications.
56
- # Note: This route will make all actions in every controller accessible via GET requests.
57
- # match ':controller(/:action(/:id))(.:format)'
14
+ resources :languages
58
15
  end
@@ -0,0 +1,9 @@
1
+ class CreateLanguages < ActiveRecord::Migration
2
+ def change
3
+ create_table :languages do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class CreateCategories < ActiveRecord::Migration
2
+ def change
3
+ create_table :categories do |t|
4
+ t.string :name
5
+ t.text :description
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ class CreatePosts < ActiveRecord::Migration
2
+ def change
3
+ create_table :posts do |t|
4
+ t.references :category
5
+ t.references :language
6
+ t.string :title
7
+ t.text :content
8
+
9
+ t.timestamps
10
+ end
11
+ add_index :posts, :category_id
12
+ add_index :posts, :language_id
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20120920161127) do
15
+
16
+ create_table "categories", :force => true do |t|
17
+ t.string "name"
18
+ t.text "description"
19
+ t.datetime "created_at", :null => false
20
+ t.datetime "updated_at", :null => false
21
+ end
22
+
23
+ create_table "languages", :force => true do |t|
24
+ t.string "name"
25
+ t.datetime "created_at", :null => false
26
+ t.datetime "updated_at", :null => false
27
+ end
28
+
29
+ create_table "posts", :force => true do |t|
30
+ t.integer "category_id"
31
+ t.integer "language_id"
32
+ t.string "title"
33
+ t.text "content"
34
+ t.datetime "created_at", :null => false
35
+ t.datetime "updated_at", :null => false
36
+ end
37
+
38
+ add_index "posts", ["category_id"], :name => "index_posts_on_category_id"
39
+ add_index "posts", ["language_id"], :name => "index_posts_on_language_id"
40
+
41
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :category do
3
+ name { Forgery(:basic).text }
4
+ description { Forgery(:basic).text }
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :language do
3
+ name { Forgery(:basic).text }
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ FactoryGirl.define do
2
+ factory :post do
3
+ category
4
+ language
5
+ title { Forgery(:basic).text }
6
+ content { Forgery(:basic).text }
7
+ end
8
+ end
@@ -0,0 +1,438 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "spec_helper"
3
+
4
+ describe SimpleResource::BaseHelper do
5
+ let(:language) { FactoryGirl.create(:language) }
6
+
7
+ before :each do
8
+ @controller = LanguagesController.new
9
+ @controller.request = ActionDispatch::TestRequest.new
10
+ @controller.instance_variable_set("@language", language)
11
+ end
12
+
13
+ describe "#resource_human_name" do
14
+ context "when resource class is defined" do
15
+ it "returns resource human name" do
16
+ helper.resource_human_name("Language").should eq("Language")
17
+ end
18
+
19
+ it "returns translated resource human name" do
20
+ set_locale
21
+ helper.resource_human_name("Language").should eq("Kieli")
22
+ reset_locale
23
+ end
24
+ end
25
+
26
+ context "when resource class is not defined" do
27
+ it "returns resource human name" do
28
+ helper.resource_human_name.should eq("Language")
29
+ end
30
+
31
+ it "returns translated resource human name" do
32
+ set_locale
33
+ helper.resource_human_name.should eq("Kieli")
34
+ reset_locale
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "#resource_title" do
40
+ it "returns resource title" do
41
+ helper.resource_title.should eq("Language #{language.id}")
42
+ end
43
+
44
+ it "returns translated resource title" do
45
+ set_locale
46
+ helper.resource_title.should eq("Kieli #{language.id}")
47
+ reset_locale
48
+ end
49
+ end
50
+
51
+ describe "#collection_title" do
52
+ it "returns collection title" do
53
+ helper.collection_title.should eq("Languages")
54
+ end
55
+
56
+ it "returns translated collection title" do
57
+ set_locale
58
+ helper.collection_title.should eq("Kielet")
59
+ reset_locale
60
+ end
61
+ end
62
+
63
+ describe "#new_resource_title" do
64
+ it "returns default new resource title" do
65
+ helper.new_resource_title.should eq("New Language")
66
+ end
67
+
68
+ it "returns translation for a new resource title" do
69
+ set_locale
70
+ helper.new_resource_title.should eq("Uusi Kieli")
71
+ reset_locale
72
+ end
73
+ end
74
+
75
+ describe "#new_resource_link" do
76
+ it "returns default version of new resource link" do
77
+ helper.new_resource_link.should eq('<a href="/languages/new">New Language</a>')
78
+ end
79
+
80
+ it "returns translated version of new resource link" do
81
+ set_locale
82
+ helper.new_resource_link.should eq('<a href="/languages/new">Uusi Kieli</a>')
83
+ reset_locale
84
+ end
85
+ end
86
+
87
+ describe "#edit_resource_title" do
88
+ it "returns default edit resource title" do
89
+ helper.edit_resource_title.should eq("Edit Language")
90
+ end
91
+
92
+ it "returns translation for a edit resource title" do
93
+ set_locale
94
+ helper.edit_resource_title.should eq("Muokkaa Kieli")
95
+ reset_locale
96
+ end
97
+ end
98
+
99
+ describe "#resource_attributes" do
100
+ it "returns resource attributes" do
101
+ helper.resource_attributes.should == ["id", "name", "created_at", "updated_at"]
102
+ end
103
+ end
104
+
105
+ describe "#non_human_attributes" do
106
+ it "returns non human attributes" do
107
+ helper.non_human_attributes == ["id", "created_at", "updated_at"]
108
+ end
109
+ end
110
+
111
+ describe "#resource_human_attributes" do
112
+ context "when resource doesn't have a parent resource" do
113
+ it "returns resource human attributes" do
114
+ helper.resource_human_attributes.should == ["name"]
115
+ end
116
+ end
117
+
118
+ context "when resource has a parent resource" do
119
+ before :each do
120
+ category = FactoryGirl.create(:category)
121
+ post = FactoryGirl.create(:post, category: category)
122
+ @controller = PostsController.new
123
+ @controller.request = ActionDispatch::TestRequest.new
124
+ @controller.instance_variable_set("@category", category)
125
+ @controller.instance_variable_set("@post", post)
126
+ end
127
+
128
+ it "returns resource human attributes" do
129
+ helper.resource_human_attributes.should == ["language_id", "title", "content"]
130
+ end
131
+ end
132
+ end
133
+
134
+ describe "#attribute_human_name" do
135
+ it "returns attribute human name" do
136
+ helper.attribute_human_name(:name).should eq("Name")
137
+ helper.attribute_human_name(:created_at).should eq("Created at")
138
+ end
139
+
140
+ it "returns translated attribute numan name" do
141
+ set_locale
142
+ helper.attribute_human_name(:name).should eq("Nimi")
143
+ helper.attribute_human_name(:created_at).should eq("Created at")
144
+ reset_locale
145
+ end
146
+
147
+ it "accepts attribute name as Symbol" do
148
+ helper.attribute_human_name(:name).should eq("Name")
149
+ end
150
+
151
+ it "accepts attribute name as String" do
152
+ helper.attribute_human_name("name").should eq("Name")
153
+ end
154
+ end
155
+
156
+ describe "#attribute_value" do
157
+ context "when attribute value is short" do
158
+ it "returns attribute value without truncation" do
159
+ helper.attribute_value(language, :name).should eq(language.name)
160
+ end
161
+ end
162
+
163
+ context "when attribute value is long" do
164
+ before :each do
165
+ @resource = FactoryGirl.create(:language)
166
+ @resource.name = Forgery(:lorem_ipsum).words(100)
167
+ end
168
+
169
+ it "returns truncated attribute value" do
170
+ expected = @resource.name.truncate(50)
171
+ helper.attribute_value(@resource, :name).should eq(expected)
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "#link_to_action" do
177
+ context "with action :show" do
178
+ it "returns show link" do
179
+ helper.link_to_action(:show, "Show", "/languages/1").should eq('<a href="/languages/1">Show</a>')
180
+ end
181
+
182
+ it "returns translated show link" do
183
+ set_locale
184
+ helper.link_to_action(:show, "Show", "/languages/1").should eq('<a href="/languages/1">Näytä</a>')
185
+ reset_locale
186
+ end
187
+ end
188
+
189
+ context "with action :edit" do
190
+ it "returns edit link" do
191
+ helper.link_to_action(:edit, "Edit", "/languages/1/edit").should eq('<a href="/languages/1/edit">Edit</a>')
192
+ end
193
+
194
+ it "returns translated edit link" do
195
+ set_locale
196
+ helper.link_to_action(:edit, "Edit", "/languages/1/edit").should eq('<a href="/languages/1/edit">Muokkaa</a>')
197
+ reset_locale
198
+ end
199
+ end
200
+
201
+ context "with action :delete" do
202
+ it "returns delete link" do
203
+ helper.link_to_action(:delete, "Delete", "/languages/1").should eq('<a href="/languages/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>')
204
+ end
205
+
206
+ it "returns translated delete link" do
207
+ set_locale
208
+ helper.link_to_action(:delete, "Delete", "/languages/1").should eq('<a href="/languages/1" data-confirm="Oletko varma?" data-method="delete" rel="nofollow">Poista</a>')
209
+ reset_locale
210
+ end
211
+ end
212
+
213
+ context "with custom action" do
214
+ it "returns link to custom action" do
215
+ helper.link_to_action(:children, "Children", "/languages/1/children").should eq('<a href="/languages/1/children">Children</a>')
216
+ end
217
+ end
218
+ end
219
+
220
+ describe "#default_actions_for" do
221
+ let(:collection) { FactoryGirl.create_list(:language, 10) }
222
+
223
+ it "returns default actions for given resource" do
224
+ collection.each do |resource|
225
+ expected = %Q(<a href="/languages/#{resource.id}">Show</a>\n)
226
+ expected += %Q(<a href="/languages/#{resource.id}/edit">Edit</a>\n)
227
+ expected += %Q(<a href="/languages/#{resource.id}" data-confirm="Are you sure?")
228
+ expected += %Q( data-method="delete" rel="nofollow">Delete</a>)
229
+ helper.default_actions_for(resource).should eq(expected)
230
+ end
231
+ end
232
+ end
233
+
234
+ describe "#controller_namespaces" do
235
+ context "when no namespaces exist" do
236
+ it "returns an empty array" do
237
+ helper.controller_namespaces.should be_empty
238
+ end
239
+ end
240
+
241
+ context "when namespace exists" do
242
+ before :each do
243
+ @controller = Backend::LanguagesController.new
244
+ @controller.request = ActionDispatch::TestRequest.new
245
+ @controller.instance_variable_set("@language", language)
246
+ end
247
+
248
+ it "returns namespace in array" do
249
+ helper.controller_namespaces.should == ["backend"]
250
+ end
251
+ end
252
+ end
253
+
254
+ describe "#resource_form_path" do
255
+ describe "new action" do
256
+ before :each do
257
+ @controller = LanguagesController.new
258
+ @controller.request = ActionDispatch::TestRequest.new
259
+ @controller.request.action = "new"
260
+ @controller.instance_variable_set("@language", Language.new)
261
+ end
262
+
263
+ context "when no namespaces exist" do
264
+ it "returns path without namespace" do
265
+ helper.resource_form_path.should eq("/languages")
266
+ end
267
+ end
268
+
269
+ context "when namespace exists" do
270
+ before :each do
271
+ @controller = Backend::LanguagesController.new
272
+ @controller.request = ActionDispatch::TestRequest.new
273
+ @controller.request.action = "new"
274
+ @language = Language.new
275
+ @controller.instance_variable_set("@language", @language)
276
+ end
277
+
278
+ it "returns path with namespace" do
279
+ helper.resource_form_path.should == ["backend", @language]
280
+ end
281
+ end
282
+ end
283
+
284
+ describe "edit action" do
285
+ before :each do
286
+ @controller = LanguagesController.new
287
+ @controller.request = ActionDispatch::TestRequest.new
288
+ @controller.request.action = "edit"
289
+ @controller.instance_variable_set("@language", language)
290
+ end
291
+
292
+ context "when no namespaces exist" do
293
+ it "returns path without namespace" do
294
+ helper.resource_form_path.should eq("/languages/#{language.id}")
295
+ end
296
+ end
297
+
298
+ context "when namespace exists" do
299
+ before :each do
300
+ @controller = Backend::LanguagesController.new
301
+ @controller.request = ActionDispatch::TestRequest.new
302
+ @controller.request.action = "edit"
303
+ @controller.instance_variable_set("@language", language)
304
+ end
305
+
306
+ it "returns path with namespace" do
307
+ helper.resource_form_path.should == ["backend", language]
308
+ end
309
+ end
310
+ end
311
+ end
312
+
313
+ describe "#render_actions_for" do
314
+ let(:collection) { FactoryGirl.create_list(:language, 10) }
315
+
316
+ it "returns actions for given resource in HTML" do
317
+ collection.each do |resource|
318
+ expected = %Q(<a href="/languages/#{resource.id}">Show</a>\n)
319
+ expected += %Q(<a href="/languages/#{resource.id}/edit">Edit</a>\n)
320
+ expected += %Q(<a href="/languages/#{resource.id}" data-confirm="Are you sure?")
321
+ expected += %Q( data-method="delete" rel="nofollow">Delete</a>\n)
322
+ helper.render_actions_for(resource).should eq(expected)
323
+ end
324
+ end
325
+ end
326
+
327
+ describe "#render_collection_table" do
328
+ before :each do
329
+ Language.destroy_all
330
+ end
331
+
332
+ let(:collection) { FactoryGirl.create_list(:language, 10) }
333
+
334
+ it "returns collection table in HTML" do
335
+ expected = %Q(<table>)
336
+ expected += %Q(<thead><tr><th>Name</th><th>&nbsp;</th></tr></thead><tbody>)
337
+
338
+ collection.each do |resource|
339
+ expected += %Q(<tr><td>#{resource.name}</td><td class="actions">)
340
+ expected += %Q(<a href="/languages/#{resource.id}">Show</a>)
341
+ expected += %Q(<a href="/languages/#{resource.id}/edit">Edit</a>)
342
+ expected += %Q(<a href="/languages/#{resource.id}" data-confirm="Are you sure?")
343
+ expected += %Q( data-method="delete" rel="nofollow">Delete</a>)
344
+ expected += %Q(</td></tr>)
345
+ end
346
+
347
+ expected += %Q(</tbody></table>)
348
+ helper.render_collection_table.gsub(/\n */, "").should eq(expected)
349
+ end
350
+ end
351
+
352
+ describe "#render_form" do
353
+ describe "new action" do
354
+ before :each do
355
+ @controller = LanguagesController.new
356
+ @controller.request = ActionDispatch::TestRequest.new
357
+ @controller.request.action = "new"
358
+ @language = Language.new
359
+ @controller.instance_variable_set("@language", @language)
360
+ end
361
+
362
+ context "with formtastic (default) builder" do
363
+ it "returns form" do
364
+ expected = <<-END
365
+ <form accept-charset="UTF-8" action="/languages" class="formtastic language"
366
+ id="new_language" method="post" novalidate="novalidate">
367
+ <div style="margin:0;padding:0;display:inline">
368
+ <input name="utf8" type="hidden" value="&#x2713;" />
369
+ </div>
370
+ <fieldset class="inputs">
371
+ <ol>
372
+ <li class="string input optional stringish" id="language_name_input">
373
+ <label class=" label" for="language_name">Name</label>
374
+ <input id="language_name" maxlength="255" name="language[name]" type="text" />
375
+ </li>
376
+ </ol>
377
+ </fieldset>
378
+ <fieldset class="actions">
379
+ <ol>
380
+ <li class="action input_action " id="language_submit_action">
381
+ <input name="commit" type="submit" value="Create Language" />
382
+ </li>
383
+ <li class="action link_action " id="language_cancel_action">
384
+ <a href="/languages">Cancel</a>
385
+ </li>
386
+ </ol>
387
+ </fieldset>
388
+ </form>
389
+ END
390
+ helper.render_form.gsub(/\n */, "").should eq(expected.gsub(/(\n|^ +)/, ""))
391
+ end
392
+ end
393
+ end
394
+
395
+ describe "edit action" do
396
+ before :each do
397
+ @controller = LanguagesController.new
398
+ @controller.request = ActionDispatch::TestRequest.new
399
+ @controller.request.action = "edit"
400
+ @controller.instance_variable_set("@language", language)
401
+ end
402
+
403
+ context "with formtastic (default) builder" do
404
+ it "returns form" do
405
+ expected = <<-END
406
+ <form accept-charset="UTF-8" action="/languages/#{language.id}" class="formtastic language"
407
+ id="edit_language_#{language.id}" method="post" novalidate="novalidate">
408
+ <div style="margin:0;padding:0;display:inline">
409
+ <input name="utf8" type="hidden" value="&#x2713;" />
410
+ <input name="_method" type="hidden" value="put" />
411
+ </div>
412
+ <fieldset class="inputs">
413
+ <ol>
414
+ <li class="string input optional stringish" id="language_name_input">
415
+ <label class=" label" for="language_name">Name</label>
416
+ <input id="language_name" maxlength="255" name="language[name]" type="text"
417
+ value="#{language.name}" />
418
+ </li>
419
+ </ol>
420
+ </fieldset>
421
+ <fieldset class="actions">
422
+ <ol>
423
+ <li class="action input_action " id="language_submit_action">
424
+ <input name="commit" type="submit" value="Update Language" />
425
+ </li>
426
+ <li class="action link_action " id="language_cancel_action">
427
+ <a href="/languages">Cancel</a>
428
+ </li>
429
+ </ol>
430
+ </fieldset>
431
+ </form>
432
+ END
433
+ helper.render_form.gsub(/\n */, "").should eq(expected.gsub(/(\n|^ +)/, ""))
434
+ end
435
+ end
436
+ end
437
+ end
438
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe "Languages" do
4
+ describe "#index" do
5
+ let!(:collection) { FactoryGirl.create_list(:language, 10) }
6
+
7
+ before :each do
8
+ visit "/languages"
9
+ end
10
+
11
+ it "has a correct title" do
12
+ page.should have_xpath "//title", text: "Languages | Dummy"
13
+ end
14
+
15
+ it "has a correct heading" do
16
+ page.should have_css "h1", "Languages"
17
+ end
18
+
19
+ it "lists all languages" do
20
+ within "table" do
21
+ within "thead" do
22
+ page.should have_css "th", text: "Name"
23
+ end
24
+ within "tbody" do
25
+ collection.each do |resource|
26
+ page.should have_css "td", text: resource.name
27
+ page.should have_link "Show", href: "/languages/#{resource.id}"
28
+ page.should have_link "Edit", href: "/languages/#{resource.id}/edit"
29
+ page.should have_link "Delete", href: "/languages/#{resource.id}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ it "has a link to create a new language" do
36
+ page.should have_link "New Language", href: "/languages/new"
37
+ end
38
+ end
39
+ end
data/spec/spec_helper.rb CHANGED
@@ -9,10 +9,11 @@ Spork.prefork do
9
9
  require "capybara/rails"
10
10
  require "capybara/rspec"
11
11
  require "database_cleaner"
12
- require "factory_girl_rails"
13
-
12
+ require "forgery"
13
+
14
+ Dir[Rails.root.join("../support/**/*.rb")].each {|f| require f}
14
15
  DatabaseCleaner.strategy = :truncation
15
-
16
+
16
17
  RSpec.configure do |config|
17
18
  config.fail_fast = true
18
19
  config.use_transactional_fixtures = false
@@ -29,11 +30,12 @@ Spork.prefork do
29
30
  config.after :each do
30
31
  DatabaseCleaner.clean
31
32
  end
33
+
34
+ config.include Helpers
32
35
  end
33
-
34
- # Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
35
36
  end
36
37
 
37
38
  Spork.each_run do
39
+ require "factory_girl_rails"
38
40
  FactoryGirl.reload
39
41
  end
@@ -0,0 +1,9 @@
1
+ module Helpers
2
+ def set_locale(locale = "fi")
3
+ I18n.locale = locale
4
+ end
5
+
6
+ def reset_locale
7
+ I18n.locale = I18n.default_locale
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-20 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -229,11 +229,13 @@ extra_rdoc_files: []
229
229
  files:
230
230
  - .gitignore
231
231
  - .rspec
232
+ - .travis.yml
233
+ - CHANGELOG.md
234
+ - DEVELOPER_GUIDE.md
232
235
  - Gemfile
233
236
  - Guardfile
234
237
  - LICENSE.txt
235
238
  - README.md
236
- - README_FOR_DEVELOPERS.md
237
239
  - Rakefile
238
240
  - app/controllers/simple_resource/base_controller.rb
239
241
  - app/helpers/simple_resource/base_helper.rb
@@ -256,9 +258,15 @@ files:
256
258
  - spec/dummy/app/assets/javascripts/application.js
257
259
  - spec/dummy/app/assets/stylesheets/application.css
258
260
  - spec/dummy/app/controllers/application_controller.rb
259
- - spec/dummy/app/helpers/application_helper.rb
261
+ - spec/dummy/app/controllers/backend/languages_controller.rb
262
+ - spec/dummy/app/controllers/categories_controller.rb
263
+ - spec/dummy/app/controllers/languages_controller.rb
264
+ - spec/dummy/app/controllers/posts_controller.rb
260
265
  - spec/dummy/app/mailers/.gitkeep
261
266
  - spec/dummy/app/models/.gitkeep
267
+ - spec/dummy/app/models/category.rb
268
+ - spec/dummy/app/models/language.rb
269
+ - spec/dummy/app/models/post.rb
262
270
  - spec/dummy/app/views/layouts/application.html.erb
263
271
  - spec/dummy/config.ru
264
272
  - spec/dummy/config/application.rb
@@ -274,8 +282,12 @@ files:
274
282
  - spec/dummy/config/initializers/secret_token.rb
275
283
  - spec/dummy/config/initializers/session_store.rb
276
284
  - spec/dummy/config/initializers/wrap_parameters.rb
277
- - spec/dummy/config/locales/en.yml
285
+ - spec/dummy/config/locales/fi.yml
278
286
  - spec/dummy/config/routes.rb
287
+ - spec/dummy/db/migrate/20120920095910_create_languages.rb
288
+ - spec/dummy/db/migrate/20120920161043_create_categories.rb
289
+ - spec/dummy/db/migrate/20120920161127_create_posts.rb
290
+ - spec/dummy/db/schema.rb
279
291
  - spec/dummy/lib/assets/.gitkeep
280
292
  - spec/dummy/log/.gitkeep
281
293
  - spec/dummy/public/404.html
@@ -283,7 +295,13 @@ files:
283
295
  - spec/dummy/public/500.html
284
296
  - spec/dummy/public/favicon.ico
285
297
  - spec/dummy/script/rails
298
+ - spec/factories/categories.rb
299
+ - spec/factories/languages.rb
300
+ - spec/factories/posts.rb
301
+ - spec/helpers/base_helper_spec.rb
302
+ - spec/integration/languages_spec.rb
286
303
  - spec/spec_helper.rb
304
+ - spec/support/helpers.rb
287
305
  homepage: https://github.com/jarijokinen/simple_resource
288
306
  licenses: []
289
307
  post_install_message:
@@ -314,9 +332,15 @@ test_files:
314
332
  - spec/dummy/app/assets/javascripts/application.js
315
333
  - spec/dummy/app/assets/stylesheets/application.css
316
334
  - spec/dummy/app/controllers/application_controller.rb
317
- - spec/dummy/app/helpers/application_helper.rb
335
+ - spec/dummy/app/controllers/backend/languages_controller.rb
336
+ - spec/dummy/app/controllers/categories_controller.rb
337
+ - spec/dummy/app/controllers/languages_controller.rb
338
+ - spec/dummy/app/controllers/posts_controller.rb
318
339
  - spec/dummy/app/mailers/.gitkeep
319
340
  - spec/dummy/app/models/.gitkeep
341
+ - spec/dummy/app/models/category.rb
342
+ - spec/dummy/app/models/language.rb
343
+ - spec/dummy/app/models/post.rb
320
344
  - spec/dummy/app/views/layouts/application.html.erb
321
345
  - spec/dummy/config.ru
322
346
  - spec/dummy/config/application.rb
@@ -332,8 +356,12 @@ test_files:
332
356
  - spec/dummy/config/initializers/secret_token.rb
333
357
  - spec/dummy/config/initializers/session_store.rb
334
358
  - spec/dummy/config/initializers/wrap_parameters.rb
335
- - spec/dummy/config/locales/en.yml
359
+ - spec/dummy/config/locales/fi.yml
336
360
  - spec/dummy/config/routes.rb
361
+ - spec/dummy/db/migrate/20120920095910_create_languages.rb
362
+ - spec/dummy/db/migrate/20120920161043_create_categories.rb
363
+ - spec/dummy/db/migrate/20120920161127_create_posts.rb
364
+ - spec/dummy/db/schema.rb
337
365
  - spec/dummy/lib/assets/.gitkeep
338
366
  - spec/dummy/log/.gitkeep
339
367
  - spec/dummy/public/404.html
@@ -341,5 +369,11 @@ test_files:
341
369
  - spec/dummy/public/500.html
342
370
  - spec/dummy/public/favicon.ico
343
371
  - spec/dummy/script/rails
372
+ - spec/factories/categories.rb
373
+ - spec/factories/languages.rb
374
+ - spec/factories/posts.rb
375
+ - spec/helpers/base_helper_spec.rb
376
+ - spec/integration/languages_spec.rb
344
377
  - spec/spec_helper.rb
378
+ - spec/support/helpers.rb
345
379
  has_rdoc:
@@ -1,2 +0,0 @@
1
- module ApplicationHelper
2
- end
@@ -1,5 +0,0 @@
1
- # Sample localization file for English. Add more files in this directory for other locales.
2
- # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
-
4
- en:
5
- hello: "Hello world"