ancestry vs. awesome_nested_set
There are several gems available for handling tree structures in Rails. This article will compare the following solutions: ancestry awesome_nested_set closure_tree ScenarioSuppose there is a model called Node that can have child nodes in a tree structure. FeaturesancestrySetupUsing the new rules, add the following to config/initializers/ancestry.rb 1Ancestry.default_ancestry_format = :materialized_path2 Tables that need to support a tree structure should include some dedicated colum ...
acts-as-taggable-on vs. PostgreSQL Array (acts-as-taggable-array-on)
acts-as-taggable-on is a commonly used gem in Rails for handling tagging functionality. However, similar features can also be implemented using PostgreSQL’s special Array type. For example, the acts-as-taggable-array-on gem utilizes PostgreSQL Arrays to achieve this. This article will compare the following solutions: acts-as-taggable-on acts-as-taggable-array-on pg_taggable ScenarioSuppose there are two models: Post and User. A Post has a tags attribute and belongs to a User. Common functional ...
jbuilder vs active_model_serializers vs oj_serializers - Rails JSON API Comparison
Rails provides a built-in as_json method for generating JSON. Additionally, jbuilder is the default gem for handling JSON APIs, and there are other alternative gems available. This article compares the following solutions: as_json jbuilder active_model_serializers oj_serializers Based on personal experience, the evaluation considers the following criteria: Ease of maintenance. Reusability of defined structures. Ability to generate JSON objects and use them within the code. Performance. Ease o ...
Load CSS Asynchronously
This article explains FOUC (Flash of unstyled content), render-blocking issues, and how to load CSS asynchronously. Flash of unstyled contentAfter the HTML is loaded, it will be displayed first, and then the CSS will be loaded. You will see the HTML screen without style first, and might load CSS style in short time. This causes it looks like a flicker. In addition to this situation, many webpages now also use JavaScript to render the screen, and similar problems will also occur. 例如一個使用 Bootstrap ...
Solidity Gas Optimizations - Function Name
DescriptionYou may not think that the name of the function will also have an impact on gas consumption. In fact, in the worst case, there may be more than a thousand of gas impacts. Let’s take a look at the following code: 1234contract Test { function b() public { }} The above execution b() will consume 125 gas, then change to the following: 1234567contract Test { function a() public { } function b() public { }} This time b() becomes 147 ga ...
How to Measure JavaScript Code Coverage With jscoverage
We had known How to Write a Library for Node.js and Browser with Mocha Unit Tests. Now, we can use some tool to calculate code coverage further. We used mocha before, so we choose jscoverage because it can support mocha. Please done your unit tests before start. Installation1npm install jscoverage --save-dev Ignored FilesIf we don’t want to include some files, we can add the list to .covignore in the root of project. And write like this: 12/tests/node_modules/ Eg. we don’t want to include test ...
How to Write a Library for Node.js and Browser with Mocha Unit Tests
Mocha is a test framework for JavaScript, it provides Node.js and browser versions. Assume we implement a JavaScript library and we want to run unit tests automatically. And also, we want that it could be used in Node.js and browsers. We can use following methods to do that. This example files structure look like this: 12345src\md5.jstests\test.jstests\node-test.jstests\index.htmlpackage.json Or refer to js-md5. InstallationInstall mocha package to your project. 12npm install mocha --save-devnpm ...
Node.js - How to Integrate Travis CI with GitHub
Travis CI is service for Continuous Integration and it is easy to integrate with the projects on GitHub. It support many programming languages. Travis CI will run unit tests after the project update and report the results. This article will introduce how to integrate node.js projects. Enable Travis CI for GitHub projectYou can sign in with GItHub account and find your projects on GitHub. Go to the Accounts page on the top-right side.Click the toggle to enable. .travis.ymlAdd .travis.yml file to ...
How to omit bundle exec prefix by bundle binstubs
bundle binstubsIn Ruby, we use Bundler to manage the gem in our projects. We also use bundle exec prefix to run the command in our projects for ensuring that it’s running in current bundler. Eg. 1bundle exec rake db:migrate But, it seems a little inconvenient so that we can use binstubs to omit it. Assume that we want to generate binstubs of rake: 1bundle binstubs rake This will generate rake script in the bin folder, and now we can run 1./bin/rake equal to 1bundle exec rake If you want to gener ...






