Skip to content

Instantly share code, notes, and snippets.

View adavia's full-sized avatar

Andrés Da Viá adavia

View GitHub Profile
@adavia
adavia / Form.js
Last active October 9, 2019 22:38
Dynamic field array react
import React, { useState } from 'react';
import { IoMdClose } from 'react-icons/io';
import { produce } from 'immer';
import { generate } from 'shortid'
const StartupForm = () => {
const [tags, setTags] = useState([
{
id: '1',
name: 'Physic'
import AuthStore from './auth';
import TopicStore from './topic';
class Store {
constructor() {
this.authStore = new AuthStore(this);
this.topicStore = new TopicStore(this);
}
}
@adavia
adavia / user.ex
Created May 15, 2018 21:10
Changeset cond validation
def changeset(%User{} = user, attrs) do
# Conditional req validation
required_fields = if attrs[:provider] do
[:username, :email]
else
[:username, :email, :password]
end
user
|> cast(attrs, @required_fields, @optional_fields)
@adavia
adavia / book_spec.rb
Last active February 28, 2018 21:48
RSpec sending raw JSON parameters post request
describe 'POST /books' do
# valid payload
let(:valid_attributes) do
# send stringify json payload
{
"title": "Learn Elm",
"user_id": user.id,
"image": "example.jpg"
}.to_json
end
@adavia
adavia / book_serializer.rb
Created February 16, 2018 19:22
AMS return obj association
class BookSerializer < ActiveModel::Serializer
attributes :id, :title, :description, :created_by, :created_at
has_many :comments
def comments
object.comments.index_by(&:id) # NoMethodError (undefined method `id' for [2]:Array):
end
end
@adavia
adavia / app.js
Last active December 20, 2017 02:54
HOC Route render
return (
<div>
<Switch location={isModal ? this.prevLocation : location}>
<Route path="/" exact component={withAuth(Main)} />
<Route path="/auth/register" exact component={Register} />
<Route path="/auth/login" exact component={Login} />
<Route path="/users" exact component={withAuth(ListUsers)} />
<Route
path='/clients/:id/edit'
render={() => withAuth(
@adavia
adavia / home.js
Created October 5, 2017 18:02
Module pattern
var Home = (function() {
var links = $("[data-behavior~=anchor-link]");
var cover = $('.cover');
var init = function() {
$(window).on("load", coverFadeOut);
}
var coverFadeOut = function() {
cover.fadeOut(1000, function() {
@adavia
adavia / errors.rb
Last active July 10, 2017 04:00
Parse errors
def parse_errors
{
errors: {
questions: @post.questions.enum_for(:each_with_index).collect { |question, index|
{
index => question.errors.messages
} unless question.valid?
}.inject({}, :merge).transform_values {|v| v.transform_values &:first }
}.merge!(@post.errors.messages)
}
@adavia
adavia / obj.js
Created July 8, 2017 08:03
object
// Change this
{
"addresses[0].city": "this is an error message"
}
// To this
{
addresses: {
0: {
city: "this is an error message"
@adavia
adavia / json.js
Created July 5, 2017 15:06
JSON API
{
result: [1, 2],
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
author: 1
},
2: {