Skip to content

Commit

Permalink
Merge pull request #19 from logicisaliar/pincode
Browse files Browse the repository at this point in the history
Create Pincode model and controller along with pincode, city and stat…
  • Loading branch information
logicisaliar authored Sep 9, 2018
2 parents fdbd4c1 + f3e40c0 commit e05e2a7
Show file tree
Hide file tree
Showing 19 changed files with 1,404 additions and 41 deletions.
18 changes: 16 additions & 2 deletions app/controllers/cities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
class CitiesController < ApplicationController

skip_before_action :authenticate_user!
before_action :set_city, only: [:update]
before_action :set_city, only: [:edit, :update]


def new
@states = class_label(State.all)
@city = City.new
end

def create
@city = City.new(city_params)
if @city.save!
redirect_to cities_path
else
render :new
end
end

def edit
@states = class_label(State.all)
end

def update
@city.update(city_params)
redirect_to cities_path
end

def index
filename = "city"
csv_read(filename)
Expand Down Expand Up @@ -42,7 +56,7 @@ 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')
csv.each do |row|
t = State.new
t = City.new
t.name = row['name']
t.state_id = row['state_id']
t.save
Expand Down
68 changes: 68 additions & 0 deletions app/controllers/pincodes_controller.rb
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

2 changes: 2 additions & 0 deletions app/models/city.rb
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
5 changes: 5 additions & 0 deletions app/models/pincode.rb
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
1 change: 1 addition & 0 deletions app/models/state.rb
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
6 changes: 6 additions & 0 deletions app/views/cities/index.html.erb
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 %>
5 changes: 5 additions & 0 deletions app/views/pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<%= link_to "New Product", new_product_path %>
<%= link_to "Items", items_path %>
<%= link_to "New Item", new_item_path %>
<%= link_to "New Pincode", new_pincode_path %>
<%= link_to "Pincodes", pincodes_path %>
<%= link_to "Cities", cities_path %>
<%= link_to "States", states_path %>

<br>
<br>
<br>
Expand Down
9 changes: 9 additions & 0 deletions app/views/pincodes/_form.html.erb
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 %>
1 change: 1 addition & 0 deletions app/views/pincodes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "form", pincode: @pincode %>
9 changes: 9 additions & 0 deletions app/views/pincodes/index.html.erb
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 %>
1 change: 1 addition & 0 deletions app/views/pincodes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "form", pincode: @pincode %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
resources :packings, only: [:index]
resources :uses, only: [:index]
resources :categories, only: [:index, :new, :create]
resources :pincodes, only: [:index, :new, :create, :edit, :update]
resources :cities, only: [:index, :new, :create]
resources :harmonics, only: [:index, :new, :create]
resources :products
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20180908110541_create_pincodes.rb
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
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_09_08_092106) do
ActiveRecord::Schema.define(version: 2018_09_08_110541) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -65,6 +65,14 @@
t.datetime "updated_at", null: false
end

create_table "pincodes", force: :cascade do |t|
t.integer "pin"
t.bigint "city_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["city_id"], name: "index_pincodes_on_city_id"
end

create_table "products", force: :cascade do |t|
t.float "discount"
t.text "description"
Expand Down Expand Up @@ -125,6 +133,7 @@
add_foreign_key "cities", "states"
add_foreign_key "items", "packings"
add_foreign_key "items", "products"
add_foreign_key "pincodes", "cities"
add_foreign_key "products", "harmonics"
add_foreign_key "products", "types"
end
Loading

0 comments on commit e05e2a7

Please sign in to comment.