The Brighton Ruby Meetup by the bins round the back of Greggs, London Road went well. Greggs ran out of Vegan pastry flakes, but Sally brought us up to speed on Class inheritance and calculating the volume of a glazed doughnut under a graph.
I’m currently trying to bring in Strong Parameters, replacing attr_accessible with a custom class for each model class we want to sanitize params for. We don’t want to fill controllers with massive blah_params methods if we’re likely to re-use them everywhere. So if we hand this over to a class that can manage it….all the better.
BlahPermit.with(param.fetch(:blah, {})).permit
I’d rather not go through every file and create an accompanying class for what, at this stage, will only require a small tweak to each attr_accessible call.
So here we can cope with both classes we can load or define on the fly if they don’t already exist.
module NotAtAllAttrAccessible
extend ActiveSupport::Concern
module ClassMethods
def attr_accessible(*attrs)
klass = define_permit_class
klass.permits += attrs
define_method :accessible_attributes do
klass.permits
end unless method_defined? :accessible_attributes
end
private
def define_permit_class
name = "#{self.name}Permit"
Object.const_get(name)
rescue NameError
Object.const_set(name, Class.new(Permit))
end
end
end
And that will equate to
class Blah
attr_accessible :thing, :the, :blair, :turnip
end
class Permit
cattr_accessor :permits, default: []
class << self
def klass(klass)
"#{klass}Permit".constantize
rescue NameError
Permit
end
def with(params)
new.with(params)
end
end
def with(params)
@params = params.is_a?(ActionController::Parameters) ? params : ActionController::Parameters.new(params)
self
end
def permit!
permits.any? ? params.permit(*permits) : params.permit!
end
def permits
self.class.permits
end
def params
@params || ActionController::Parameters.new
end
end
class BlahPermit < Permit
@@permits = [:thing, :the, :blair, :turnip]
end
And we can then crush Parameters like a boss.
def resource_params
Permit.klass(Blah).with(param.fetch(:blah, {})).permit
end
/Users/rl/.node-gyp/16.5.0/include/node/v8-internal.h:454:38: error: no template named 'remove_cv_t' in namespace 'std'; did you mean 'remove_cv'?
!std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
~~~~~^~~~~~~~~~~
remove_cv
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/type_traits:776:50: note: 'remove_cv' declared here
template <class _Tp> struct _LIBCPP_TEMPLATE_VIS remove_cv
^
1 error generated.
make: *** [Release/obj.target/binding/src/binding.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/Users/rl/repos/rostering/vendor/engines/sard_core/ui/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack at ChildProcess.emit (node:events:394:28)
gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
gyp ERR! System Darwin 20.5.0
gyp ERR! command "/usr/local/Cellar/node/16.5.0/bin/node" "/Users/rl/repos/rostering/vendor/engines/sard_core/ui/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /Users/rl/repos/rostering/vendor/engines/sard_core/ui/node_modules/node-sass
gyp ERR! node -v v16.5.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Yeah, srsly dude. This isn’t right for us either. I think that brew upgrade I did last night broke something, must have upgraded node. Apparently upgrading beyond node-sass 6.0.1 is fine now. Last time this happened I had to downgrade node.