Skip to content

Commit

Permalink
Merge pull request #2 from shivam091/0.2.0
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
shivam091 committed May 15, 2023
2 parents 816acb2 + df89d01 commit ebf225d
Show file tree
Hide file tree
Showing 30 changed files with 629 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rails_bootstrap_form (0.1.1)
rails_bootstrap_form (0.2.0)

GEM
remote: http://rubygems.org/
Expand Down
4 changes: 4 additions & 0 deletions demo/.byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
c
@bootstrap_form_options
c
@bootstrap_form_options
70 changes: 70 additions & 0 deletions demo/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class UsersController < ApplicationController

before_action :find_user, except: [:index, :new, :create]

def index
@users = ::User.all
end

def new
@user = ::User.new
end

def create
@user = ::User.new(user_params)
if @user.save
redirect_to users_path
else
render :new, status: :unprocessable_entity
end
end

def edit
end

def update
if @user.update(user_params)
redirect_to users_path
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@user.destroy
redirect_to users_path
end

private

def user_params
params.require(:user).permit(
:name,
:email,
:password,
:mobile_number,
:birth_date,
:terms,
:excellence,
:blog_url,
:fruit_id,
:favorite_color,
skill_ids: [],
address_attributes: [
:street,
:state,
:city,
:postal_code,
:country_id,
]
)
end

def find_user
@user = ::User.find(params.fetch(:id))
end
end
10 changes: 10 additions & 0 deletions demo/app/models/address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class Address < ApplicationRecord
belongs_to :user
belongs_to :country

delegate :name, to: :country, prefix: true
end
17 changes: 17 additions & 0 deletions demo/app/models/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class Country < ApplicationRecord
DEFAULT_OPTIONS = [
"India",
"Ireland",
"United States",
"United Kingdom",
"Spain",
"France",
"Canada"
].freeze

has_many :addresses, dependent: :restrict_with_exception
end
14 changes: 14 additions & 0 deletions demo/app/models/fruit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class Fruit < ApplicationRecord
DEFAULT_OPTIONS = [
"Mango",
"Apple",
"Orange",
"Watermelon"
].freeze

has_many :users, dependent: :restrict_with_exception
end
21 changes: 21 additions & 0 deletions demo/app/models/skill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class Skill < ApplicationRecord
DEFAULT_OPTIONS = [
"Communication",
"Problem Solving",
"Leadership",
"Writing",
"Creativity",
"Time Management",
"Team Work",
"Negotiation",
"Decision Making",
"Management"
].freeze

has_many :user_skills, dependent: :restrict_with_exception
has_many :users, through: :user_skills, source: :user
end
20 changes: 20 additions & 0 deletions demo/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class User < ApplicationRecord
has_one :address, dependent: :destroy

has_many :user_skills, dependent: :destroy
has_many :skills, through: :user_skills, source: :skill

belongs_to :fruit

delegate :name, to: :fruit, prefix: true

accepts_nested_attributes_for :address, update_only: true

def address
super.presence || build_address
end
end
8 changes: 8 additions & 0 deletions demo/app/models/user_skill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class UserSkill < ApplicationRecord
belongs_to :user
belongs_to :skill
end
5 changes: 5 additions & 0 deletions demo/app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if false %>
<%= render partial: "users/form_without_bootstrap_helpers" %>
<% end %>
<%= render partial: "users/vertical_form" %>
49 changes: 49 additions & 0 deletions demo/app/views/users/_form_without_bootstrap_helpers.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div class="card card-primary my-3">
<div class="card-header fw-bold">
Profile Form (Without RailsBootstrapForm)
</div>
<div class="card-body">
<%= form_for @user do |form| %>
<%= form.text_field :name, autocomplete: "new-name" %>
<%= form.email_field :email %>
<%= form.password_field :password, autocomplete: "new-password" %>
<%= form.phone_field :mobile_number %>
<%= form.date_field :birth_date %>
<%= form.check_box :terms %>
<%= form.range_field :excellence %>
<%= form.url_field :blog_url %>
<%= form.url_field :favorite_color %>
<%= form.select :fruit_id, options_for_select(::Fruit.pluck(:name, :id), form.object.fruit_id), include_blank: "Select Favorite Fruit" %>
<%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name do |b| %>
<%= b.check_box + b.text %>
<% end %>
<%= form.fields_for :address, include_id: false do |address_form| %>
<%= address_form.text_area :street %>
<%= address_form.text_field :state %>
<%= address_form.text_field :city %>
<%= address_form.text_field :postal_code %>
<%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id), include_blank: "Select Country" %>
<% end %>
<div class="mt-3">
<%= form.submit "Register", class: "btn btn-primary" %>
<%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
</div>
<% end %>
</div>
</div>

<div class="card card-primary">
<div class="card-header fw-bold">
Login Form (Without RailsBootstrapForm)
</div>
<div class="card-body">
<%= form_for @user do |form| %>
<%= form.email_field :email %>
<%= form.password_field :password, autocomplete: "new-password" %>
<div class="mt-3">
<%= form.submit "Login", class: "btn btn-primary" %>
<%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
</div>
<% end %>
</div>
</div>
33 changes: 33 additions & 0 deletions demo/app/views/users/_vertical_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="card card-primary my-3">
<div class="card-header fw-bold">
Profile Form (Vertical layout)
</div>
<div class="card-body">
<%= bootstrap_form_for @user, bootstrap_form: {} do |form| %>
<%= form.text_field :name, autocomplete: "new-name", required: true, bootstrap_form: {} %>
<%= form.text_field :email, autocomplete: "new-email", bootstrap_form: {} %>
<%= form.text_field :password, autocomplete: "new-password" %>
<%= form.phone_field :mobile_number %>
<%= form.date_field :birth_date %>
<%= form.check_box :terms %>
<%= form.range_field :excellence %>
<%= form.url_field :blog_url %>
<%= form.url_field :favorite_color %>
<%= form.select :fruit_id, options_for_select(::Fruit.pluck(:name, :id), form.object.fruit_id), include_blank: "Select Favorite Fruit" %>
<%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name do |b| %>
<%= b.check_box + b.text %>
<% end %>
<%= form.fields_for :address, include_id: false do |address_form| %>
<%= address_form.text_area :street %>
<%= address_form.text_field :state %>
<%= address_form.text_field :city %>
<%= address_form.text_field :postal_code %>
<%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id), {include_blank: "Select Country", bootstrap: {}} %>
<% end %>
<div class="mt-3">
<%= form.submit "Register", class: "btn btn-primary" %>
<%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
</div>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions demo/app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form" %>
42 changes: 42 additions & 0 deletions demo/app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<div class="card card-primary">
<div class="card-header">
<div class="text-end">
<%= link_to "New user", new_user_path, class: "btn btn-primary" %>
</div>
</div>
<div class="card-body">
<div class="table-responsive mt-3">
<table class="table table-bordered table-stripped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Mobile number</th>
<th scope="col">Favorite Fruit</th>
<th scope="col">Country</th>
<th scope="col" width="15%">Actions</th>
</tr>
</thead>
<tbody>
<% @users.each.with_index(1) do |user, index| %>
<tr style="vertical-align: middle;">
<td><%= index %></td>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.mobile_number %></td>
<td><%= user.fruit_name %></td>
<td><%= user.address.country_name %></td>
<td>
<%= link_to "Edit", edit_user_path(user), class: "btn btn-sm btn-secondary" %>
<div class="d-inline-block">
<%= button_to "Delete", user, class: "btn btn-sm btn-danger", method: :delete %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions demo/app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form" %>
3 changes: 3 additions & 0 deletions demo/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
# -*- warn_indent: true -*-

Rails.application.routes.draw do
root to: "users#index"

resources :users
end
12 changes: 12 additions & 0 deletions demo/db/migrate/20230514054308_create_countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class CreateCountries < ActiveRecord::Migration[7.0]
def change
create_table :countries do |t|
t.string :name
t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions demo/db/migrate/20230514054851_create_fruits.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class CreateFruits < ActiveRecord::Migration[7.0]
def change
create_table :fruits do |t|
t.string :name
t.timestamps
end
end
end
21 changes: 21 additions & 0 deletions demo/db/migrate/20230514055237_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.string :mobile_number
t.string :blog_url
t.date :birth_date
t.references :fruit
t.boolean :terms
t.string :excellence
t.string :favorite_color
t.timestamps
end
end
end
17 changes: 17 additions & 0 deletions demo/db/migrate/20230514055840_create_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class CreateAddresses < ActiveRecord::Migration[7.0]
def change
create_table :addresses, id: false do |t|
t.references :user, primary_key: true
t.references :country
t.string :street
t.string :city
t.string :state
t.string :postal_code
t.timestamps
end
end
end
Loading

0 comments on commit ebf225d

Please sign in to comment.