Skip to content

Commit e818c0c

Browse files
Wriiten the controller Specs ref #3
Also set the basic Layout Please enter the commit message for your changes. Lines starting
1 parent 50da28c commit e818c0c

File tree

11 files changed

+312
-21
lines changed

11 files changed

+312
-21
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ group :development, :test do
7979
gem 'simplecov'
8080
end
8181

82-
# Use to count the successful operations in mongodb
82+
gem 'simple_form'

Gemfile.lock

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ GEM
188188
json
189189
redis (>= 3.0.6)
190190
redis-namespace (>= 1.3.1)
191+
simple_form (3.0.2)
192+
actionpack (~> 4.0)
193+
activemodel (~> 4.0)
191194
simplecov (0.8.2)
192195
docile (~> 1.1.0)
193196
multi_json
@@ -247,6 +250,7 @@ DEPENDENCIES
247250
sdoc (~> 0.4.0)
248251
sendgrid
249252
sidekiq
253+
simple_form
250254
simplecov
251255
spring
252256
therubyracer

app/views/layouts/application.html.haml

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414

1515
%ul.nav.navbar-nav
1616
%li.active
17-
%a{href: "#"}
17+
%a{href: "#{home_index_path}"}
1818
%h4 Home
1919
%li
2020
%a{href: "#"}
2121
%h4 Newsletters
2222
%li
23-
%a{href: "#"}
24-
%h4 Create New
23+
%a{href: "#{new_user_path}"}
24+
%h4 Add New User
25+
%li
26+
%a{href: "#{users_path}"}
27+
%h4 All Users
28+
2529

2630
%ul.nav.navbar-nav.navbar-right
2731
%li.active

app/views/users/_form.html.haml

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
= form_for @user do |f|
2-
Email: #{f.text_field :email_id}
3-
Subcription: #{f.text_field :is_subscribed}
4-
Joined: #{f.text_field :joined_on}
5-
Source: #{f.text_field :source}#{f.submit "Add User Id"}
1+
= simple_form_for (@user) do |f|
2+
.inputs
3+
= f.input :email_id
4+
= f.input :is_subscribed
5+
= f.input :joined_on
6+
= f.input :source
7+
= f.button :submit
8+

app/views/users/index.html.haml

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
2-
3-
4-
5-
6-
7-
8-
9-
10-
11-
%table
1+
%table.table.table-striped
122
- @users.each do |user|
133
%tr
144
%td
@@ -18,4 +8,3 @@
188
%td
199
= link_to "Delete", user_path(user), method: :delete
2010
%tr
21-
= link_to "Add User Id", new_user_path

config/initializers/simple_form.rb

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Use this setup block to configure all options available in SimpleForm.
2+
SimpleForm.setup do |config|
3+
# Wrappers are used by the form builder to generate a
4+
# complete input. You can remove any component from the
5+
# wrapper, change the order or even add your own to the
6+
# stack. The options given below are used to wrap the
7+
# whole input.
8+
config.wrappers :default, class: :input,
9+
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
10+
## Extensions enabled by default
11+
# Any of these extensions can be disabled for a
12+
# given input by passing: `f.input EXTENSION_NAME => false`.
13+
# You can make any of these extensions optional by
14+
# renaming `b.use` to `b.optional`.
15+
16+
# Determines whether to use HTML5 (:email, :url, ...)
17+
# and required attributes
18+
b.use :html5
19+
20+
# Calculates placeholders automatically from I18n
21+
# You can also pass a string as f.input placeholder: "Placeholder"
22+
b.use :placeholder
23+
24+
## Optional extensions
25+
# They are disabled unless you pass `f.input EXTENSION_NAME => :lookup`
26+
# to the input. If so, they will retrieve the values from the model
27+
# if any exists. If you want to enable the lookup for any of those
28+
# extensions by default, you can change `b.optional` to `b.use`.
29+
30+
# Calculates maxlength from length validations for string inputs
31+
b.optional :maxlength
32+
33+
# Calculates pattern from format validations for string inputs
34+
b.optional :pattern
35+
36+
# Calculates min and max from length validations for numeric inputs
37+
b.optional :min_max
38+
39+
# Calculates readonly automatically from readonly attributes
40+
b.optional :readonly
41+
42+
## Inputs
43+
b.use :label_input
44+
b.use :hint, wrap_with: { tag: :span, class: :hint }
45+
b.use :error, wrap_with: { tag: :span, class: :error }
46+
end
47+
48+
# The default wrapper to be used by the FormBuilder.
49+
config.default_wrapper = :default
50+
51+
# Define the way to render check boxes / radio buttons with labels.
52+
# Defaults to :nested for bootstrap config.
53+
# inline: input + label
54+
# nested: label > input
55+
config.boolean_style = :nested
56+
57+
# Default class for buttons
58+
config.button_class = 'btn'
59+
60+
# Method used to tidy up errors. Specify any Rails Array method.
61+
# :first lists the first message for each field.
62+
# Use :to_sentence to list all errors for each field.
63+
# config.error_method = :first
64+
65+
# Default tag used for error notification helper.
66+
config.error_notification_tag = :div
67+
68+
# CSS class to add for error notification helper.
69+
config.error_notification_class = 'alert alert-error'
70+
71+
# ID to add for error notification helper.
72+
# config.error_notification_id = nil
73+
74+
# Series of attempts to detect a default label method for collection.
75+
# config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
76+
77+
# Series of attempts to detect a default value method for collection.
78+
# config.collection_value_methods = [ :id, :to_s ]
79+
80+
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
81+
# config.collection_wrapper_tag = nil
82+
83+
# You can define the class to use on all collection wrappers. Defaulting to none.
84+
# config.collection_wrapper_class = nil
85+
86+
# You can wrap each item in a collection of radio/check boxes with a tag,
87+
# defaulting to :span. Please note that when using :boolean_style = :nested,
88+
# SimpleForm will force this option to be a label.
89+
# config.item_wrapper_tag = :span
90+
91+
# You can define a class to use in all item wrappers. Defaulting to none.
92+
# config.item_wrapper_class = nil
93+
94+
# How the label text should be generated altogether with the required text.
95+
# config.label_text = lambda { |label, required| "#{required} #{label}" }
96+
97+
# You can define the class to use on all labels. Default is nil.
98+
config.label_class = 'control-label'
99+
100+
# You can define the class to use on all forms. Default is simple_form.
101+
# config.form_class = :simple_form
102+
103+
# You can define which elements should obtain additional classes
104+
# config.generate_additional_classes_for = [:wrapper, :label, :input]
105+
106+
# Whether attributes are required by default (or not). Default is true.
107+
# config.required_by_default = true
108+
109+
# Tell browsers whether to use the native HTML5 validations (novalidate form option).
110+
# These validations are enabled in SimpleForm's internal config but disabled by default
111+
# in this configuration, which is recommended due to some quirks from different browsers.
112+
# To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
113+
# change this configuration to true.
114+
config.browser_validations = false
115+
116+
# Collection of methods to detect if a file type was given.
117+
# config.file_methods = [ :mounted_as, :file?, :public_filename ]
118+
119+
# Custom mappings for input types. This should be a hash containing a regexp
120+
# to match as key, and the input type that will be used when the field name
121+
# matches the regexp as value.
122+
# config.input_mappings = { /count/ => :integer }
123+
124+
# Custom wrappers for input types. This should be a hash containing an input
125+
# type as key and the wrapper that will be used for all inputs with specified type.
126+
# config.wrapper_mappings = { string: :prepend }
127+
128+
# Default priority for time_zone inputs.
129+
# config.time_zone_priority = nil
130+
131+
# Default priority for country inputs.
132+
# config.country_priority = nil
133+
134+
# When false, do not use translations for labels.
135+
# config.translate_labels = true
136+
137+
# Automatically discover new inputs in Rails' autoload path.
138+
# config.inputs_discovery = true
139+
140+
# Cache SimpleForm inputs discovery
141+
# config.cache_discovery = !Rails.env.development?
142+
143+
# Default class for inputs
144+
# config.input_class = nil
145+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Use this setup block to configure all options available in SimpleForm.
2+
SimpleForm.setup do |config|
3+
config.wrappers :bootstrap, tag: 'div', class: 'control-group', error_class: 'error' do |b|
4+
b.use :html5
5+
b.use :placeholder
6+
b.use :label
7+
b.wrapper tag: 'div', class: 'controls' do |ba|
8+
ba.use :input
9+
ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
10+
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
11+
end
12+
end
13+
14+
config.wrappers :prepend, tag: 'div', class: "control-group", error_class: 'error' do |b|
15+
b.use :html5
16+
b.use :placeholder
17+
b.use :label
18+
b.wrapper tag: 'div', class: 'controls' do |input|
19+
input.wrapper tag: 'div', class: 'input-prepend' do |prepend|
20+
prepend.use :input
21+
end
22+
input.use :hint, wrap_with: { tag: 'span', class: 'help-block' }
23+
input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
24+
end
25+
end
26+
27+
config.wrappers :append, tag: 'div', class: "control-group", error_class: 'error' do |b|
28+
b.use :html5
29+
b.use :placeholder
30+
b.use :label
31+
b.wrapper tag: 'div', class: 'controls' do |input|
32+
input.wrapper tag: 'div', class: 'input-append' do |append|
33+
append.use :input
34+
end
35+
input.use :hint, wrap_with: { tag: 'span', class: 'help-block' }
36+
input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
37+
end
38+
end
39+
40+
# Wrappers for forms and inputs using the Twitter Bootstrap toolkit.
41+
# Check the Bootstrap docs (http://twitter.github.com/bootstrap)
42+
# to learn about the different styles for forms and inputs,
43+
# buttons and other elements.
44+
config.default_wrapper = :bootstrap
45+
end

config/locales/simple_form.en.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
en:
2+
simple_form:
3+
"yes": 'Yes'
4+
"no": 'No'
5+
required:
6+
text: 'required'
7+
mark: '*'
8+
# You can uncomment the line below if you need to overwrite the whole required html.
9+
# When using html, text and mark won't be used.
10+
# html: '<abbr title="required">*</abbr>'
11+
error_notification:
12+
default_message: "Please review the problems below:"
13+
# Labels and hints examples
14+
# labels:
15+
# defaults:
16+
# password: 'Password'
17+
# user:
18+
# new:
19+
# email: 'E-mail to sign in.'
20+
# edit:
21+
# email: 'E-mail.'
22+
# hints:
23+
# defaults:
24+
# username: 'User name to sign in.'
25+
# password: 'No special characters, please.'
26+

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Rails.application.routes.draw do
22
resources :users
33
resources :newsletters
4+
resources :home
45
root :to => "home#index"
56

67
# The priority is based upon order of creation: first created -> highest priority.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
= simple_form_for(@<%= singular_table_name %>) do |f|
2+
= f.error_notification
3+
4+
.form-inputs
5+
<%- attributes.each do |attribute| -%>
6+
= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
7+
<%- end -%>
8+
9+
.form-actions
10+
= f.button :submit
+64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
11
require 'rails_helper'
22

33
RSpec.describe UsersController, :type => :controller do
4+
5+
context "Get index" do
6+
it "list of user email" do
7+
get :index
8+
expect(response).to render_template("index")
9+
end
10+
end
411

12+
context "GET show" do
13+
let(:user) {FactoryGirl.create(:user)}
14+
it "show the details of the users" do
15+
get :show, {:id => user.id}
16+
expect(response).to render_template("show")
17+
end
18+
end
19+
20+
context "GET new" do
21+
let(:new_user) {FactoryGirl.attributes_for(:user)}
22+
it "display new form for adding user" do
23+
post :new, {user: new_user}
24+
expect(response).to render_template("new")
25+
end
26+
end
27+
28+
context " POST create" do
29+
let(:new_user) {FactoryGirl.attributes_for(:user)}
30+
it "creates a new user" do
31+
post :create, {user: new_user}
32+
expect(response).to redirect_to(users_path)
33+
end
34+
it "not arise" do
35+
post :create, {user: new_user}
36+
expect(response).not_to render_template("new")
37+
end
38+
end
39+
40+
context "GET edit" do
41+
let(:new_user) {FactoryGirl.create(:user)}
42+
it "fetches the specific all the users" do
43+
get :edit, {:id => new_user.id}
44+
expect(response).to render_template("edit")
45+
end
46+
end
47+
48+
49+
context "DELETE delete" do
50+
let(:user) {FactoryGirl.create(:user)}
51+
it "delete an user" do
52+
delete :destroy, {:id => user.id}
53+
expect(response).to redirect_to(users_path)
54+
end
55+
end
56+
57+
58+
context "PUT update" do
59+
let(:new_user) {FactoryGirl.create(:user)}
60+
it "updates details of specified user" do
61+
patch :update, {:id =>new_user.id, :user => { :email_id => new_user.email_id}}
62+
expect(response).to redirect_to(users_path)
63+
end
64+
it " not arise" do
65+
put :update, {:id =>new_user.id, :user => { :email => new_user.email_id}}
66+
expect(response).not_to render_template("edit")
67+
end
68+
end
569
end

0 commit comments

Comments
 (0)