Skip to content

Commit

Permalink
chore: Refactor enum definitions to Rails 8 format
Browse files Browse the repository at this point in the history
  • Loading branch information
mromulus committed Oct 31, 2024
1 parent 3586b34 commit 93a2838
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 52 deletions.
5 changes: 1 addition & 4 deletions app/models/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ class Actor < ApplicationRecord
has_ancestry

attribute :default_visibility, :integer # rails bug during migrations
enum default_visibility: {
public: 1,
actor_only: 2
}, _prefix: :default_visibility
enum :default_visibility, { public: 1, actor_only: 2 }, prefix: :default_visibility

belongs_to :exercise, touch: true

Expand Down
4 changes: 2 additions & 2 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Address < ApplicationRecord

delegate :exercise, to: :network

enum mode: {
enum :mode, {
ipv4_static: 1,
ipv4_dhcp: 2,
ipv6_static: 3,
Expand All @@ -21,7 +21,7 @@ class Address < ApplicationRecord
ipv6_uniqlocal: 7,
ipv4_vip: 8,
ipv6_vip: 9
}, _prefix: :mode, _default: 'ipv4_static'
}, prefix: :mode, default: 'ipv4_static'

scope :all_ip_objects, -> {
includes(:virtual_machine, :network)
Expand Down
12 changes: 2 additions & 10 deletions app/models/address_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ class AddressPool < ApplicationRecord
has_one :exercise, through: :network
has_many :addresses

enum ip_family: {
v4: 1,
v6: 2,
}, _prefix: :ip, _default: 'v4'

enum scope: {
default: 1,
mgmt: 2,
other: 999,
}, _prefix: :scope, _default: 'default'
enum :ip_family, { v4: 1, v6: 2 }, prefix: :ip, default: 'v4'
enum :scope, { default: 1, mgmt: 2, other: 999 }, prefix: :scope, default: 'default'

validates :name, :ip_family, :scope, presence: true
validates :network_address, format: {
Expand Down
15 changes: 3 additions & 12 deletions app/models/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@ class Check < ApplicationRecord
belongs_to :destination, polymorphic: true
has_one :exercise, through: :service

enum protocol: Protocols.to_enum_hash, _prefix: :protocol

enum ip_family: {
v4v6: 0,
v4: 1,
v6: 2
}, _prefix: :ip_family

enum check_mode: {
network: 1,
special: 2
}, _prefix: :check_mode
enum :protocol, Protocols.to_enum_hash, prefix: :protocol
enum :ip_family, { v4v6: 0, v4: 1, v6: 2 }, prefix: :ip_family
enum :check_mode, { network: 1, special: 2 }, prefix: :check_mode

def self.to_icon
'fa-flask'
Expand Down
5 changes: 1 addition & 4 deletions app/models/customization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ class CustomizationSpec < ApplicationRecord

friendly_id :api_id, use: [:slugged, :scoped], scope: :virtual_machine

enum mode: {
host: 1,
container: 2
}, _prefix: :mode, _default: 'host'
enum :mode, { host: 1, container: 2 }, prefix: :mode, default: 'host'

scope :for_api, -> {
includes(
Expand Down
5 changes: 1 addition & 4 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ class Exercise < ApplicationRecord
friendly_id :abbreviation, use: :slugged
has_paper_trail

enum mode: {
exercise: 1,
infra: 2
}, _prefix: :mode
enum :mode, { exercise: 1, infra: 2 }, prefix: :mode

has_many :actors, dependent: :destroy
has_many :role_bindings, dependent: :destroy
Expand Down
5 changes: 1 addition & 4 deletions app/models/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ class Network < ApplicationRecord
friendly_id :slug_candidates, use: [:slugged, :scoped], scope: :exercise
has_paper_trail

enum visibility: {
public: 1,
actor_only: 2
}, _prefix: :visibility
enum :visibility, { public: 1, actor_only: 2 }, prefix: :visibility

belongs_to :exercise
belongs_to :actor
Expand Down
4 changes: 2 additions & 2 deletions app/models/role_binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class RoleBinding < ApplicationRecord
belongs_to :exercise
belongs_to :actor, optional: true

enum role: {
enum :role, {
environment_member: 1,
environment_net_dev: 2,
environment_service_dev: 3,
environment_admin: 4,
actor_readonly: 5,
actor_dev: 6
}, _prefix: :role
}, prefix: :role

scope :for_user, ->(user) {
where(user_email: user.email)
Expand Down
11 changes: 1 addition & 10 deletions app/models/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ class VirtualMachine < ApplicationRecord
include SpecCacheUpdateBeforeDestroy
has_paper_trail

# TO BE REMOVED
enum deploy_mode: {
single: 0,
bt: 1
}, _prefix: :deploy_mode

enum visibility: {
public: 1,
actor_only: 2
}, _prefix: :visibility
enum :visibility, { public: 1, actor_only: 2 }, prefix: :visibility

belongs_to :exercise
belongs_to :actor
Expand Down

0 comments on commit 93a2838

Please sign in to comment.