Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "features": {
- "utilities": {
- "gas": false,
- "water": false,
- "electric": true
- }
- }
- }
- ActiveAdmin.register Entity do
- permit_params :name, :config, :message, :water, :gas, :electric
- form do |f|
- f.inputs do
- f.input :name
- f.input :water, as: :text, :as => :select, :collection => [true, false]
- f.input :gas, as: :text, :as => :select, :collection => [true, false]
- f.input :electric, as: :text, :as => :select, :collection => [true, false]
- end
- f.actions
- end
- index do
- column :id
- column :parent_id
- column :name
- [:water, :gas, :electric].each do |u|
- column u do |e|
- e.send("has_#{u}?")
- end
- column "parent_#{u}" do |e|
- e.parent.nil? ? nil : e.parent.send("has_#{u}?")
- end
- end
- actions
- end
- show do
- attributes_table do
- row :name
- [:water, :gas, :electric].each do |u|
- row u do |e|
- e.send("has_#{u}?")
- end
- end
- end
- end
- preserve_default_filters!
- remove_filter :name, :salesforce_id, :subdomain, :code, :display_name, :kind, :category, :status, :status_updated_at, :live_date, :cancelled_date, :test, :subscription_plan_id, :discount_id, :email, :proper_email, :first_name, :last_name, :phone, :external_id, :external_office_id, :address_id, :moves_per_month, :number_of_units, :pmm, :stripe_customer_id, :stripe_subscription_id, :stripe_subscription_metadata, :subscription_type, :logo_id, :facebook_url, :twitter_url, :linkedin_url, :website, :team_name, :message, :enable_automated_invites, :notify_before_invite, :notify_after_invite, :force_hard_verification, :automated, :integration_status, :last_synced_at, :deleted_at, :created_at, :updated_at
- sidebar :remove_utilities do
- form title: 'from google sheet' do |f|
- end
- end
- end
- class Entity < ActiveRecord::Base
- belongs_to :parent, class_name: 'Entity'
- def has_config
- self.has_attribute? 'config'
- end
- def water
- has_water?
- end
- def water=(val)
- set_water(val)
- end
- def gas
- has_gas?
- end
- def gas=(val)
- set_gas(val)
- end
- def electric
- has_electric?
- end
- def electric=(val)
- set_electric(val)
- end
- def utility(util)
- self[:config].try(:fetch, "features", nil).try(:fetch, "utilities", nil).try(:fetch, "#{util}", nil)
- end
- def has_water?
- utility("water") == true
- end
- def has_gas?
- utility("gas") == true
- end
- def has_electric?
- utility("electric") == true
- end
- private
- def default_config
- {"features" => {"utilities" => {"water" => true, "gas" => true, "electric" => true}}}
- end
- def make_explicit(util)
- self[:config].reverse_merge!(default_config) unless util
- end
- def set_water(boolean)
- new_val = cleaned_boolean(boolean)
- make_explicit(water)
- self[:config]["features"]["utilities"]["water"] = new_val
- end
- def set_gas(boolean)
- new_val = cleaned_boolean(boolean)
- make_explicit(gas)
- self[:config]["features"]["utilities"]["gas"] = new_val
- end
- def set_electric(boolean)
- new_val = cleaned_boolean(boolean)
- make_explicit(electric)
- self[:config]["features"]["utilities"]["electric"] = new_val
- end
- def cleaned_boolean(val)
- if val.is_a?(String)
- return true if val.downcase == "true"
- return false
- elsif val.is_a?(TrueClass) or val.is_a?(FalseClass)
- return val
- else
- fail 'Must be String or Boolean'
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement