nguyeness
nguyeness
John Nguyen
5 posts
A digital chef who is passionate about user experience, clean semantic code and who fully embraces collaboration Beyond my enthusiasm for making, I enjoy cooking and eating, jogging, and the performing arts.
Don't wanna be here? Send us removal request.
nguyeness 5 years ago
Text
Building Vim from source
Get the latest ruby and python installation. At the time of this post, ruby is at v2.7 and python is at v3.8.
shell git clone https://github.com/vim/vim.git ~/.config/vim cd ~/.config/vim ./configure --with-features=huge \ --without-x \ --enable-multibyte \ --enable-rubyinterp=yes \ --enable-python3interp=yes \ --with-python3-config-dir=$(python3-config --configdir) \ --enable-perlinterp=yes \ --enable-luainterp=yes \ --enable-cscope \ --prefix=/usr/local sudo make sudo make install sudo make clean
0 notes
nguyeness 5 years ago
Text
Missing respond_to method in Rails 6 API only app
For Rails 6 API mode only, the respond_to method is missing. Say you wanted to use Devise for user authentication; you'll need to have it respond to the json format.
# config/routes.rb scope :api, defaults { format: :json } do devise_for :users end
There we have the routes scoped to the /api namespace and restricting the format to only json requests.
In the controller is there we'll set the respond_to :json.
# app/controllers/application_controller.rb class ApplicationController < ActionController::API respond_to :json end
This will complain about the missing respond_to method. Resolving this issue is by simply inlcuding the MimeResponds class.
# app/controllers/application_controller.rb class ApplicationController < ActionController::API include ActionController::MimeResponds respond_to :json end
This was documentated in the Rails API.
0 notes
nguyeness 5 years ago
Text
Enabling cookie sessions for Rails 6 api only
As referenced in the guide:
4.4 Other Middleware
Rails ships with a number of other middleware that you might want to use in an API application, especially if one of your API clients is the browser:
Rack::MethodOverride ActionDispatch::Cookies ActionDispatch::Flash For session management ActionDispatch::Session::CacheStore ActionDispatch::Session::CookieStore ActionDispatch::Session::MemCacheStore
We'll need to add these back in manually.
# config/application.rb ... module App class Application < Rails::Application config.load_defaults 6.0 config.api_only = true config.middleware.use ActionDispatch::Cookies config.middleware.use ActionDispatch::Session::CookieStore, key: "custom_cookie_session_name", expires_after: 14.days end end
For more details regarding the session cookie store, check out the API documentation. That will reference the rack session abstract persisted class for the available options to use when instantiating the session cookie store.
0 notes
nguyeness 6 years ago
Text
Wiping a disk with zeros or random data with `dd`
Wiping a disk with zeros:
dd if=/dev/zero of=/dev/sdX bs=8M
Wiping a disk with random data:
dd if=/dev/urandom of=/dev/sdX bs=8M
Note: X in sdX needs to be replaced with the drive to be wiped. The usual case is a. Another thing to note is that bs should be close to the disk cache size for optimial performance.
To determine optimial speed, run:
dd if=/dev/urandom of=zero bs=512K; rm zero; dd if=/dev/urandom of=zero bs=1M; rm zero; dd if=/dev/urandom of=zero bs=8M; rm zero; dd if=/dev/urandom of=zero bs=32M; rm zero; dd if=/dev/urandom of=zero bs=1G; rm zero;
Add as many different block size (bs) variants to test for optimial speeds.
0 notes
nguyeness 8 years ago
Text
Implementing Rails STI and organizing subclasses
I won鈥檛 be talking about why or when to use Single Table Inheritance鈥搕his article is about how to implement and organize your files.
Let鈥檚 start off by creating a base class:
bin/rails genereate model Fruit name:string origin:string amount:integer type:string
For instance, a Gala apple鈥搕he name would be Gala and the type would be apple.
Let's create the Apple subclass:
bin/rails generate model Fruit::Apple --parent Fruit
By default rails will use the type column in Fruit to identify which subclass it should use; but since I'm namespacing our subclasses with Fruit:: I'll need to update the Fruit base class to know how to identify it's subclasses.
# app/models/fruit.rb ... def self.find_sti_class(type_name) type_name = if type_name =~ /fruit/i "#{self}::#{self.name}" else self.name end super end ...
Then in the subclasses, in this case the Apple class:
# app/models/fruit/apple.rb class Fruit::Apple < Fruit def self.sti_name 'Apple' end end
Now, define the scopes in the base model for ease of querying via the base model, Fruit:
# app/models/fruit.rb ... scope :apples, -> { where(type: 'Apple') } ...
That's pretty much it.
0 notes