-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from logicisaliar/pincode
Create Pincode model and controller along with pincode, city and stat…
- Loading branch information
Showing
19 changed files
with
1,404 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class PincodesController < ApplicationController | ||
|
||
skip_before_action :authenticate_user! | ||
before_action :set_pincode, only: [:edit, :update] | ||
|
||
def new | ||
@pincode = Pincode.new | ||
end | ||
|
||
def create | ||
@pincode = Pincode.new(pincode_params) | ||
if @pincode.save! | ||
redirect_to pincodes_path | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
@pincode.update(pincode_params) | ||
redirect_to pincode_path | ||
end | ||
|
||
def index | ||
filename = "pincode" | ||
csv_read(filename) | ||
@pincodes = Pincode.all | ||
end | ||
|
||
private | ||
|
||
def pincode_params | ||
params.require(:pincode).permit(:pin, :city_id) | ||
end | ||
|
||
def set_pincode | ||
@pincode = Pincode.find(params[:id]) | ||
end | ||
|
||
# def class_label(cls) | ||
# return_array = [] | ||
# sorted = cls.sort_by &:name | ||
# sorted.each do |p| | ||
# return_array << [p.id, "#{p.name} (#{p.code}) "] | ||
# end | ||
# return_array | ||
# end | ||
|
||
def csv_read(filename) | ||
csv_text = File.read(Rails.root.join('lib', 'seeds', "#{filename}.csv")) | ||
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') | ||
count = 0 | ||
csv.each do |row| | ||
t = Pincode.new | ||
t.pin = row['pin'] | ||
t.city_id = row['city_id'] | ||
if (count > 100) | ||
end | ||
t.save | ||
count += 1 | ||
end | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
class City < ApplicationRecord | ||
validates_uniqueness_of :name, scope: :state_id | ||
belongs_to :state | ||
has_many :pincodes | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Pincode < ApplicationRecord | ||
belongs_to :city | ||
delegate :state, :to => :city, :allow_nil => true | ||
validates :pin, uniqueness: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
class State < ApplicationRecord | ||
validates :name, uniqueness: true | ||
has_many :pincodes, :through => :cities | ||
has_many :cities | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Cities</h1> | ||
<% @cities.each do |c| %> | ||
<%= c.id %> | ||
<%= c.name %> | ||
<%= c.state.name %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>Pincode</h1> | ||
<%= simple_form_for pincode do |f| %> | ||
<%= f.error_notification %> | ||
<%= f.input :pin %> | ||
<%= f.input :city_id, collection: City.all %> | ||
<%= f.button :submit %> | ||
<% end %> | ||
|
||
<%= link_to "Go back", root_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render "form", pincode: @pincode %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>Pincodes</h1> | ||
<% @pincodes.each do |p| %> | ||
<%= p.pin %> | ||
<%= p.city.name %> | ||
<%= p.state.name %> | ||
<br> | ||
<% end %> | ||
|
||
<%= link_to "Go back", root_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render "form", pincode: @pincode %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreatePincodes < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :pincodes do |t| | ||
t.integer :pin | ||
t.references :city, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.