Skip to content

Commit

Permalink
Merge pull request #33 from JuzerShakir/pg-enums
Browse files Browse the repository at this point in the history
Pg Enums
  • Loading branch information
JuzerShakir authored Jan 3, 2024
2 parents 2f5dfc0 + feae54a commit 2b72cd2
Show file tree
Hide file tree
Showing 34 changed files with 191 additions and 70 deletions.
10 changes: 3 additions & 7 deletions app/controllers/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ def sabeels

@apts = {}

APARTMENTS.each do |apartment|
Sabeel::APARTMENTS.each do |apartment|
total_sabeels = Sabeel.send(apartment)
active_thaalis = total_sabeels.taking_thaali
inactive = total_sabeels.not_taking_thaali
@apts[apartment] = {}
@apts[apartment].store(:active_thaalis, active_thaalis.length)
@apts[apartment].store(:total_sabeels, total_sabeels.length)
@apts[apartment].store(:inactive_thaalis, inactive.length)
SIZES.each do |size|
@apts[apartment].store(size.to_sym, active_thaalis.with_thaali_size(size).length)
end
Thaali::SIZES.each { @apts[apartment].store(_1, active_thaalis.with_thaali_size(_1).length) }
end
end

Expand All @@ -32,9 +30,7 @@ def thaalis
@years[y].store(:count, thaalis.count)
@years[y].store(:pending, Thaali.dues_unpaid_for(y).length)
@years[y].store(:complete, Thaali.dues_cleared_in(y).length)
SIZES.each do |size|
@years[y].store(size.to_sym, thaalis.send(size).count)
end
Thaali::SIZES.each { @years[y].store(_1, thaalis.send(_1).count) }
end
end
end
4 changes: 1 addition & 3 deletions app/controllers/thaalis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def new

if @sabeel.took_thaali?
took_thaali = @thaalis.where(year: PREV_YR).first
@thaali.number = took_thaali[:number]
@thaali.size = took_thaali[:size]
@thaali.attributes = took_thaali.slice(:number, :size)
end
end

Expand All @@ -24,7 +23,6 @@ def edit
def create
@sabeel = Sabeel.find(params[:sabeel_id])
@thaali = @sabeel.thaalis.new(create_params)
@thaali.year = CURR_YR

if @thaali.save
Rails.cache.write("sabeel_#{@sabeel.id}_taking_thaali?", true)
Expand Down
5 changes: 5 additions & 0 deletions app/models/concerns/array_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ArrayExtensions
refine Array do
def to_h_titleize_value = to_h { [_1, _1.to_s.titleize] }
end
end
13 changes: 6 additions & 7 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
class Role < ApplicationRecord
# * Constants
NAMES = %w[admin member viewer]

# rubocop:disable Rails/HasAndBelongsToMany
has_and_belongs_to_many :users, join_table: :users_roles
# rubocop:enable Rails/HasAndBelongsToMany

belongs_to :resource,
polymorphic: true,
optional: true
belongs_to :resource, polymorphic: true, optional: true

validates :resource_type,
inclusion: {in: Rolify.resource_types},
allow_nil: true
validates :resource_type, inclusion: {in: Rolify.resource_types}, allow_nil: true

validates :name, presence: true
validates :name, inclusion: {in: ROLES}
validates :name, inclusion: {in: NAMES}

scopify
end
10 changes: 9 additions & 1 deletion app/models/sabeel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class Sabeel < ApplicationRecord
# * Constants
APARTMENTS = %i[mohammedi taiyebi burhani maimoon_a maimoon_b]

# * Defaults
attribute :apartment, default: nil

# * Associations
has_many :thaalis, dependent: :destroy
has_many :transactions, through: :thaalis
Expand All @@ -21,8 +27,10 @@ def sluggables = [its]
include ITSValidation
include NameValidation

using ArrayExtensions

# * Enums
enum :apartment, APARTMENTS
enum apartment: APARTMENTS.to_h_titleize_value

# * Scopes
scope :no_thaali, -> { where.missing(:thaalis) }
Expand Down
11 changes: 10 additions & 1 deletion app/models/thaali.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
class Thaali < ApplicationRecord
# * Constants
SIZES = %i[small medium large]

attr_readonly :total, :year

# * Defaults
attribute :year, default: CURR_YR
attribute :size, default: nil

# * Associtions
belongs_to :sabeel
has_many :transactions, dependent: :destroy
Expand All @@ -16,8 +23,10 @@ class Thaali < ApplicationRecord

def sluggables = [year, number]

using ArrayExtensions

# * Enums
enum :size, SIZES
enum size: SIZES.to_h_titleize_value

# * Scopes
scope :dues_cleared_in, ->(year) {
Expand Down
10 changes: 9 additions & 1 deletion app/models/transaction.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
class Transaction < ApplicationRecord
# * Constants
MODES = %i[cash cheque bank]

default_scope { order(date: :desc) }

# * Defaults
attribute :mode, default: nil

# * Associations
belongs_to :thaali

Expand All @@ -16,8 +22,10 @@ class Transaction < ApplicationRecord

def sluggables = [recipe_no]

using ArrayExtensions

# * Enums
enum :mode, MODES
enum mode: MODES.to_h_titleize_value

# * Scopes
scope :that_occured_on, ->(date) { where(date: date) }
Expand Down
4 changes: 1 addition & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def is?(type) = role == type

# * Custom Validations
def must_have_a_role
unless roles.any?
errors.add(:role_ids, "selection is required")
end
errors.add(:role_ids, "selection is required") unless roles.any?
end
end
2 changes: 1 addition & 1 deletion app/views/sabeels/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= f.error_notification %>
<%= f.input :its %>
<%= f.input :name, placeholder: "Juzer Shabbir Shakir" %>
<%= f.input :apartment, collection: APARTMENTS.map { [_1.to_s.titleize, _1] }, as: :radio_buttons %>
<%= f.input :apartment, collection: Sabeel.apartments.invert.to_a, as: :radio_buttons %>
<%= f.input :flat_no %>
<%= f.input :mobile, placeholder: "9876543210" %>
<%= f.input :email %>
Expand Down
7 changes: 3 additions & 4 deletions app/views/thaalis/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<div class="container col-xxl-6 col-xl-7 col-lg-8 col-md-11 col-sm-10 pt-4 p-5 bg-white bg-opacity-50 shadow-lg border border-2 border-success-subtle rounded-4 mb-5">
<%= simple_form_for @thaali, url: url_helper do |f| %>
<%= simple_form_for @thaali, url: path do |f| %>
<%= f.error_notification %>

<%# for new thaali it will show current year %>
<%= f.input :year, disabled: true, input_html: {value: (action_name == "edit") ? @thaali.year : CURR_YR} %>
<%= f.input :year, disabled: true %>
<%= f.input :number %>
<%= f.input :size, collection: SIZES.map { [_1.capitalize, _1] }, as: :radio_buttons %>
<%= f.input :size, collection: Thaali.sizes.invert.to_a, as: :radio_buttons %>
<%= f.input :total, disabled: action_name == "edit" %>
<%= f.button :submit, action_name: %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/thaalis/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

<h2 class="heading mb-3">Edit Thaali</h2>

<%= render "form", url_helper: thaali_path %>
<%= render "form", path: thaali_path %>
2 changes: 1 addition & 1 deletion app/views/thaalis/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

<h2 class="heading mb-3">New Thaali</h2>

<%= render "form", url_helper: sabeel_thaalis_path %>
<%= render "form", path: sabeel_thaalis_path %>
2 changes: 1 addition & 1 deletion app/views/transactions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= f.error_notification %>
<%= f.input :recipe_no %>
<%= f.input :amount, hint: "Amount shouldn't be greater than: ₹#{total_balance}" %>
<%= f.input :mode, collection: MODES.map { [_1.capitalize, _1] }, as: :radio_buttons %>
<%= f.input :mode, collection: Transaction.modes.invert.to_a, as: :radio_buttons %>
<%= f.input :date, order: %i[day month year] %>
<%= f.button :submit, action_name: %>
<% end %>
Expand Down
5 changes: 0 additions & 5 deletions config/initializers/constant.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
# ! Add new apartments, sizes & modes to the END of the list
APARTMENTS = %i[mohammedi taiyebi burhani maimoon_a maimoon_b]
SIZES = %i[small medium large]
MODES = %i[cash cheque bank]
ROLES = %w[admin member viewer]
CURR_YR = 2022
PREV_YR = CURR_YR - 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ReplaceApartmentIntegerWithApartmentEnumTypeInSabeel < ActiveRecord::Migration[7.1]
def change
apartments = ["Mohammedi", "Taiyebi", "Burhani", "Maimoon A", "Maimoon B"]
create_enum :apartments, apartments

add_column :sabeels, :apartment_enum, :enum, enum_type: :apartments, default: "Mohammedi", null: false

reversible do |direction|
# rubocop:disable Rails/SkipsModelValidations
direction.up do
apartments.each_with_index do |apartment, i|
Sabeel.where(apartment: i).update_all(apartment_enum: apartment)
end
end

direction.down do
apartments.each_with_index do |apartment, i|
Sabeel.where(apartment_enum: apartment).update_all(apartment: i)
end
# to access `#apartment` data, update the enum value of it in `app/models/sabeels` file:
# enum apartment: APARTMENTS
end
# rubocop:enable Rails/SkipsModelValidations
end

remove_column :sabeels, :apartment, :integer
rename_column :sabeels, :apartment_enum, :apartment
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ReplaceSizeIntegerWithSizeEnumTypeInThaali < ActiveRecord::Migration[7.1]
def change
sizes = %w[Small Medium Large]
create_enum :sizes, sizes

add_column :thaalis, :size_enum, :enum, enum_type: :sizes, default: "Small", null: false

reversible do |direction|
# rubocop:disable Rails/SkipsModelValidations
direction.up do
sizes.each_with_index do |size, i|
Thaali.where(size: i).update_all(size_enum: size)
end
end

direction.down do
sizes.each_with_index do |size, i|
Thaali.where(size_enum: size).update_all(size: i)
end
# to access `#size` data, update the enum value of it in `app/models/thaalis` file:
# enum size: SIZES
end
# rubocop:enable Rails/SkipsModelValidations
end

remove_column :thaalis, :size, :integer
rename_column :thaalis, :size_enum, :size
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ReplaceModeIntegerWithModeEnumTypeInTransaction < ActiveRecord::Migration[7.1]
def change
modes = %w[Cash Cheque Bank]
create_enum :modes, modes

add_column :transactions, :mode_enum, :enum, enum_type: :modes, default: "Cash", null: false

reversible do |direction|
# rubocop:disable Rails/SkipsModelValidations
direction.up do
modes.each_with_index do |mode, i|
Transaction.where(mode: i).update_all(mode_enum: mode)
end
end

direction.down do
modes.each_with_index do |mode, i|
Transaction.where(mode_enum: mode).update_all(mode: i)
end
# to access `#mode` data, update the enum value of it in `app/models/transactions` file:
# enum mode: MODES
end
# rubocop:enable Rails/SkipsModelValidations
end

remove_column :transactions, :mode, :integer
rename_column :transactions, :mode_enum, :mode
end
end
14 changes: 10 additions & 4 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2b72cd2

Please sign in to comment.