Skip to content

Instantly share code, notes, and snippets.

View thiagoa's full-sized avatar

Thiago Araújo Silva thiagoa

  • thoughtbot
  • Natal / RN - Brazil
View GitHub Profile
Imageable = Module.new
def Imageable.included(model_class)
model_class.has_many :images
end
Imageable.module_eval do
def main_image
images.find { |image| image.kind == 'main_image' }
end
class Project < ApplicationRecord
include Imageable
end
project = Project.new
project.main_image #=> <Image kind="main_image" imageable_type="Project">
module Imageable
# When including this module, this hook is called
# by Ruby with the included module/class
def self.included(model_class)
model_class.has_many :images
end
def main_image
images.find { |image| image.kind == 'main_image' }
end
export default function wrapSentencesInTags(text) {
return text
.split('\n')
.map(chunk => chunk.trim())
.map(chunk => (chunk !== '') ? `<p>${chunk}</p>` : '')
.join('');
}
import { expect } from 'chai';
import renderOneTweet from 'app/twitter/renderOneTweet';
import createFixture from '../../support/createFixture';
describe('renderOneTweet', () => {
let fixture;
beforeEach(() => { fixture = createFixture({ html: '<div id="fixture"></div>' }); });
afterEach(() => { fixture.remove(); });
import parseOneTweet from 'app/twitter/parseOneTweet'; // Add this line
import renderOneTweet from 'app/twitter/renderOneTweet';
import fetchTimeline from 'app/twitter/fetchTimeline';
export default function mountComponent(opts) {
const containerNode = opts.containerNode;
function renderTweets(response) {
const html = response
.tweets
import { pipe } from 'lodash/fp';
import wrapSentencesInTags from 'app/twitter/wrapSentencesInTags';
import parseLineBreaks from 'app/twitter/parseLineBreaks';
import parseTweetMentions from 'app/twitter/parseTweetMentions';
const parseText = pipe(parseTweetMentions, wrapSentencesInTags, parseLineBreaks);
export default function parseOneTweet(tweet) {
return Object.assign({}, tweet, { text: parseText(tweet) });
}
// app/assets/javascripts/app/twitter/parseOneTweet.js
export default function parseOneTweet(tweet) {
return tweet;
}
import { expect } from 'chai';
import parseOneTweet from 'app/twitter/parseOneTweet';
describe('parseOneTweet', () => {
it('returns a tweet object with parsed text', () => {
const tweet = {
created_at: '2016-01-01T00:00:00.000-03:00',
mentions: ['dude'],
text: '@dude 1 shot of Tequila\n\n2 shots of Tequila...\n3 shots of Tequila...',
};
function replaceMention(text, mention) {
const regexp = new RegExp(`^@${mention}\\b|(\\s)@${mention}\\b`, 'g');
const template = `$1<a href="#" data-js-mention>@${mention}</a>`;
return text.replace(regexp, template);
}
export default function parseTweetMentions({ text, mentions }) {
return mentions.reduce(replaceMention, text);
}