Proper identification of admin for admin_data on Heroku - ruby-on-rails

We have everything installed correctly, but when an admin goes to /admin_data it throws "not authorized".
heres the relevant code in config/initializers/admin_data.rb
AdminData.config do |config|
config.is_allowed_to_view = lambda {|controller| return true if current_user.admin = true }
config.is_allowed_to_update = lambda {|controller| return true if current_user.admin = true }
end

Could be the condition you're using to check equality:
if current_user.admin = true #will always be true
vs
if current_user.admin == true #will check the equality of being true
You might consider just:
if current_user.admin
since nil or false will be NOT == true

Related

rails changed? method is always false

I am trying to check in my model if a checkbox value was changed or not. If it was changed I want my method set_ip_setting to run in before_save but my x variable is always coming back as false even if I change the value. Why is it coming back as always false? I don't see what I am doing wrong
before_save :set_ip_setting, if: :check_my_ip
def check_my_ip
x = self.sip.my_ip_changed?
is_ip = self.sip.my_ip
if x == true && is_ip == true
return false
elsif x == false && is_ip == true
return false
elsif x == true
return true
end
end
def set_ip_setting
if self.sip.try(:my_ip) == true
sip.nat = "yes"
sip.invite = "yes"
end
end
EDITED
I got my desired outcome by using previous_changes method but I think there is a cleaner way but here is what worked out for me
def check_my_ip
x = self.sip.previous_changes[:my_ip]
off_to_on = [false, true]
on_to_off = [true, false]
is_ip = self.sip.my_ip ? 1 : 0
if x.nil? && is_ip == 1
return false
elsif x == on_to_off
return false
end
return true
end
Generally you do not write if x == true. In Ruby, true and false are distinct values. For example, 1 == true is false.
Instead, simply check if x or if !x.
def check_my_ip
# Note that an explicit `self` is not necessary except when setting a value.
x = sip.my_ip_changed?
is_ip = sip.my_ip
if x && is_ip
return false
elsif !x && is_ip
return false
elsif x
return true
end
end
def set_ip_setting
if self.sip.try(:my_ip)
sip.nat = "yes"
sip.invite = "yes"
end
end
Your logic can be simplified, and implicit return used.
def check_my_ip
x = sip.my_ip_changed?
is_ip = sip.my_ip
if is_ip
false
elsif x
true
else
false
end
end

Rails logic not firing on `false` results

Receiving as an API an array of hashes
#request['clients'].each do |client|
validations are being executed on each clientattributes. However, rails logic is failing to fire up on false statements and this ignoring them. Example validation:
def client_type_ok
if #this_client['type'] == "ADT" || #this_client['type'] == "CHD"
true
else
false
#error_code_39 = true
end
end
The controller action wants to execute only when true conditions are met:
if client_type_ok && client_type_ok
However Rails.logger is clearly confirming that this condition is being passed through although false.
Rails.logger.info !#this_client['type'].blank?
Rails.logger.info #this_client['type']
Rails.logger.info "not"
Rails.logger.info #this_client['type'] != "ADT"
Rails.logger.info "ADT"
Rails.logger.info #this_client['type'] == "ADT"
is returning
true
APT
not
true
ADT
` `
The bottom is generated as a blank. The same occurs replacing Rails.logger with p. All logic of this action is ignoring false results. While I can attempt to devise processing cases of fully true cases, this is inconvenient and counter-intuitive.
Thus, there appears to be a meta function which is impeding the handling of false cases. How can this be tracked down? Can Rails.logger logic be step traced?
You're not returning false there
def client_type_ok
if #this_client['type'] == "ADT" || #this_client['type'] == "CHD"
true
else
false # this is not doing anything. Simply ignored.
#error_code_39 = true # result of this assignment will be true.
# and it also is the last expression of the if
# so it becomes the implicit return value of the method
end
end
def client_type_ok
if #this_client['type'] == "ADT" || #this_client['type'] == "CHD"
true
else
false
#error_code_39 = true
end
end
As Sergio mentiond in the above answer,The return value of yur method will be true for the else condtion. You need to swap the places or You can rewrite the above method
def client_type_ok
return true if %w(ADT CHD).include?(#this_client['type'])
#error_code_39 = true
false
end

im having troubles getting my toggle script working in roblox

i need help
this is the script:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
end
if a == false then
a = true
script.Parent.Click:Play()
end
end)
this is the hirachy:
https://imgur.com/a/4FXHY
but NOTHING happens, no errors either, except for the click sound playing
i seriously need help
The problem is is that after you do a == true, you set a to false, which a == false then matches after.
You can solve this with an if then else end statement, like so:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
else
a = true
script.Parent.Click:Play()
end
end)
However, this will only change the local value of a, which means that it will not save the change.
To fix this, we need to assign the game.Workspace.LogRideToggleds value of Value directly, which we can do like:
script.Parent.Parent.Activated:connect(function()
if game.Workspace.LogRideToggled.Value == true then
game.Workspace.LogRideToggled.Value = false
script.Parent.Click:Play()
else
game.Workspace.LogRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Although it's bad practice to repeatedly index this like this, so we can store game.Workspace.LogRideToggled in a local variable. You can read up on why this works but storing value doesn't here
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value == true then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Also, the == true is redundant, as Lua expects a truthy or falsey value as the condition, all == true does in this case is give true or false if it's true of false.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Yet we can clean this up a bit more, as we use script.Parent.Click:Play() in both cases, and we can replace logRideToggled.Value = with a logical not, like so.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
-- Todo if truthy
else
-- Todo if falsey
end
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
But if you only want to toggle this value, not do anything special for either case, we can remove that entire conditional, leaving:
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
Hope this has helped!

helper method returning true when it should return false

In my application, 'Users' has_many 'Jobs', through 'applications'
I'm trying to create a helper method has_job(#user, #job). Where, it will return true if the user has already associated itself to a particular job.
When I'm doing this though, if I apply to 1 job, then it returns true for all other jobs.
Why is this happening?
This is what my helper method looks like ->
def has_job(user,current_job)
if user.applications.any?
user.applications.each do |application|
return true if application.job_id = current_job.id
end
end
false
end
return true if application.job_id == current_job.id
The single = would cause it to return true every time.

How to implement custom validations for methods called by other methods?

I create a Subscription for a User via the method .create_subscription(plan_title).
That method checks that it's possible to subscribe (not oversubscribed plan or archived subscription) via the method .plan_subscribable?(plan).
This method does either return true or false, but I would like it to return an error message as well that could be passed to the user if false.
How and where do I implement these validation errors?
class User
def plan_subscribable?(plan)
users_subscribed = Subscription.where(plan_id: plan.id).size
return false unless users_subscribed <= plan.quantity
return false unless plan.archived == false
return true
end
def create_subscription(plan_title)
plan = Plan.where(title: plan_title).first
if plan_subscribable?(plan)
Subscription.create(...)
end
end
end
You could modify plan_subscribable? to return a boolean true or a string containing the specific error message:
def plan_subscribable?(plan)
return 'The number of users cannot be higher than the plan quantity' unless users_subscribed <= plan.quantity
return 'Archived plans are not subscribable' unless plan.archived == false
return true
end
Then, evaluate whether the returned value from plan_subscribable? is true. If it is not, the statement is implicitly false and you can use the returned value as the error message:
def create_subscription(plan_title)
plan = Plan.where(title: plan_title).first
subscribable_or_error = plan_subscribable?(plan)
if subscribable_or_error === true
Subscription.create(...)
else
error_message = subscribable_or_error
end
end

Resources