This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Project < ApplicationRecord | |
| include Imageable | |
| end | |
| project = Project.new | |
| project.main_image #=> <Image kind="main_image" imageable_type="Project"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // app/assets/javascripts/app/twitter/parseOneTweet.js | |
| export default function parseOneTweet(tweet) { | |
| return tweet; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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...', | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |