|
| 1 | +# Hashtags |
| 2 | + |
| 3 | +[](https://travis-ci.org/tomasc/hashtags) [](http://badge.fury.io/rb/hashtags) [](https://coveralls.io/r/tomasc/hashtags) |
| 4 | + |
| 5 | +Set of models and JS plugins to facilitate inline text hashtags. |
| 6 | + |
| 7 | +* hashtags are entered inline in text, for example as `@tomasc`, `#location:Home(12345)` or `$my_variable` |
| 8 | +* when rendered, they are replaced by actual values, for example HTML tags, images, links etc. |
| 9 | + |
| 10 | +Additionally: |
| 11 | + |
| 12 | +* the user can be assisted with a dropdown triggered by a special character (`#`, `@`, `$`, …) |
| 13 | +* `has_hash_tag_field` method is available to assist when rendering HTML input fields incl. data attributes, help, and supporting only specified hashtag classes |
| 14 | +* hashtags typically have `cache_key` defined on class so corresponding fragment cache can be easily expired |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +Add this line to your application's Gemfile: |
| 19 | + |
| 20 | +```ruby |
| 21 | +gem 'hashtags' |
| 22 | +``` |
| 23 | + |
| 24 | +And then execute: |
| 25 | + |
| 26 | + $ bundle |
| 27 | + |
| 28 | +Or install it yourself as: |
| 29 | + |
| 30 | + $ gem install hashtags |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +The hashtags have the following structure: |
| 35 | + |
| 36 | +``` |
| 37 | +<trigger>(type)(human_id)<value> |
| 38 | +``` |
| 39 | + |
| 40 | +The three following types are included in this gem, however the gem's design allows for definition of new types and their subclassing. |
| 41 | + |
| 42 | +### Hash tag types |
| 43 | + |
| 44 | +#### User hash tag |
| 45 | + |
| 46 | +``` |
| 47 | +@tomasc |
| 48 | +``` |
| 49 | + |
| 50 | +#### Resource hash tag |
| 51 | + |
| 52 | +``` |
| 53 | +#location:Home(12345) |
| 54 | +``` |
| 55 | + |
| 56 | +#### Variable hash tag |
| 57 | + |
| 58 | +``` |
| 59 | +$number_of_users |
| 60 | +``` |
| 61 | + |
| 62 | +### Adding new hash tags in your application |
| 63 | + |
| 64 | +The following are quick examples. It is advised to read the source code of `Hashtags::User`, `Hashtags::Resource` and `Hashtags::Variable` and their superclass `Hashtags::Builder` in order to fully understand the possibilities and flexibility of this gem. |
| 65 | + |
| 66 | +#### User |
| 67 | + |
| 68 | +See `Hashtags::User` and override its methods on your subclass as necessary. Typically it would be at least the following: |
| 69 | + |
| 70 | +```ruby |
| 71 | +class UserTag < Hashtags::User |
| 72 | + def self.resource_class |
| 73 | + # User |
| 74 | + end |
| 75 | + |
| 76 | + def self.resources_for_query(query) |
| 77 | + # User.where(full_name: /#{query}/i) |
| 78 | + end |
| 79 | + |
| 80 | + def resource_as_json(resource) |
| 81 | + # { |
| 82 | + # _id: resource.id.to_s, |
| 83 | + # to_s: resource.full_name, |
| 84 | + # human_id: resource.full_name |
| 85 | + # } |
| 86 | + end |
| 87 | +end |
| 88 | +``` |
| 89 | + |
| 90 | +#### Resource |
| 91 | + |
| 92 | +See `Hashtags::Resource` and override its methods on your subclass as necessary. Typically it would be at least the following: |
| 93 | + |
| 94 | +```ruby |
| 95 | +class LocationTag < Hashtags::Resource |
| 96 | + def self.resource_class |
| 97 | + # Location |
| 98 | + end |
| 99 | + |
| 100 | + def self.resources_for_query(query) |
| 101 | + # Location.where(name: /#{query}/i) |
| 102 | + end |
| 103 | + |
| 104 | + def resource_as_json(resource) |
| 105 | + # { |
| 106 | + # _id: resource.id.to_s, |
| 107 | + # to_s: resource.name, |
| 108 | + # human_id: resource.name |
| 109 | + # } |
| 110 | + end |
| 111 | +end |
| 112 | +``` |
| 113 | + |
| 114 | +#### Variable |
| 115 | + |
| 116 | +See `Hashtags::Variable` and override its methods on your subclass as necessary. Typically it would be at least the following: |
| 117 | + |
| 118 | +```ruby |
| 119 | +class LocationTag < Hashtags::Resource |
| 120 | + def self.values(_hash_tag_classes) |
| 121 | + # %w( |
| 122 | + # variable_1 |
| 123 | + # variable_2 |
| 124 | + # ) |
| 125 | + end |
| 126 | + |
| 127 | + def markup(match) |
| 128 | + # case name(match) |
| 129 | + # when 'variable_1' then get_value_of_variable_1(match) |
| 130 | + # when 'variable_2' then get_value_of_variable_2(match) |
| 131 | + # end |
| 132 | + end |
| 133 | +end |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +## Development |
| 138 | + |
| 139 | +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 140 | + |
| 141 | +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 142 | + |
| 143 | +## Contributing |
| 144 | + |
| 145 | +Bug reports and pull requests are welcome on GitHub at https://github.com/tomasc/hashtags. |
| 146 | + |
| 147 | + |
| 148 | +## License |
| 149 | + |
| 150 | +The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). |
0 commit comments