Thursday 10 September 2020

It used to be before Rails 5 that a third party gem would be needed for attachments.  These gems include Paperclip, Carierwave, and Dragonfly.   Active Storage is now built into rails by default.

It still needs to be invoked to install it in a project, however.  The below command will create a migration.  More info on Active Storage can be found https://edgeguides.rubyonrails.org/active_storage_overview.html

bin/rails active_storage:install

rake db:migrate

Tuesday 1 September 2020

For jest tests, an easy way to skip a test is to use skip i.e.

describe.skip('My great test section', () => { test('A test'), () => { .... }); });

Monday 17 July 2017

To provide a way of extending the touch range of a React-native component, one can use the hitSlop property, for example:

<TouchableOpacity hitSlop={{left: 20, right: 20, top: 20, bottom: 20}}
                  testID={testID} style={styles.eyeBtn}
                  onPress={e => this.handleToggleSecure(e)}>

Wednesday 26 June 2017

When a unhandled rejection in a promise is thrown in node it does not by default tell you where the error in your code is. This is quite frustrating.  In order to see more details about where the error is being created one can use a process.on callback.  For example:

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here
});

This technique was originally found on this Stack Overflow page.

Wednesday 31 May 2017

New await, async for Javascript.  By creating a function as async we can now await any promises so that the next line will not get executed until the await statement has been resolved.  Below is an example:

async function read () {
 let html = await getRandomArticle(); //We stop here until the article is retrieved.
 let md = hget(html, {
    markdown: true,
    root: 'main',
    ignore: '.at-subscribe,.mm-comments,.de-sidebar'
 });
 var txt = marked(md, { renderer: new Term() });
 console.log(txt);
}

React Native

Run react native packager with simulator for ios:

make platform=ios sim

For Android apps:

make planform=android sim

To map network ports for Android studio:

adb reverse tcp:9080 tcp:9080

Wednesday 12 April 2017

Ruby

Detect returns the first item in the list for which the block returns TRUE. Example:

>> [1,2,3,4,5,6,7].detect { |x| x.between?(3,4) }
=> 3

Returns 3 because that is the first item in the list that returns TRUE for the expression x.between?(3,4).

detect stops iterating after the condition returns true for the first time. select will iterate until the end of the input list is reached and returns all of the items where the block returned true.

Friday 30 March 2017

Ruby on Rails

Use find_by in controllers to find a relevant Active Record, will take a hash and find by multiple params, won’t break with nils and blanks. Click here for further info.

Thursday 29 March 2017

Ruby on Rails

Concerns can be used to share functionality between models.  I had been using module includes  in a lib directory to do this previously, and this is a better way of achieving the same result using an inbuilt Rails mechanism.  There is a great introduction to this concept by Vaidehi Joshi here.

Modules can be extended and included so what’s the difference?   Well extending a module is used when when we want to use extend class methods i.e. “def self.myMyClassMethod” ones.  I found this page useful for illustrating the differences between them and how they can be used.

You can use the included method as a callback so whenever the module is included in another class this callback will be triggered. Here is an example of this use.

Another cool things about the extends keyword is that you can use to extend a particular instance object without affecting any other instances.  For example:

irb(main):001:0> module Greeter; def goodbye; "Bye!"; end; end
=> nil
irb(main):002:0> String.extend(Greeter)
=> String
irb(main):003:0> String.goodbye
=> "Bye!"
irb(main):004:0> x = "foo"
=> "foo"
irb(main):005:0> x.goodbye
NoMethodError: undefined method `goodbye' for "foo":String
        from (irb):5
        from :0
irb(main):006:0>