Class: Tweet
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tweet
- Includes:
- BelongsToEntity
- Defined in:
- app/models/tweet.rb
Class Method Summary collapse
- .chronological ⇒ Object
- .create_for(entity) ⇒ Object
- .create_tweet(params) ⇒ Object
- .created_chronological ⇒ Object
- .created_reverse_chronological ⇒ Object
- .find_for(entity) ⇒ Object
- .not_disabled ⇒ Object
- .options_for(entity) ⇒ Object
- .reverse_chronological ⇒ Object
- .styles(entity) ⇒ Object
- .tweet_for(entity) ⇒ Object
- .tweeted ⇒ Object
- .untweeted ⇒ Object
- .update_tweet(params) ⇒ Object
Instance Method Summary collapse
- #allow_resend_tweet? ⇒ Boolean
- #allow_save? ⇒ Boolean
- #allow_tweet_now? ⇒ Boolean
- #auto_tweet! ⇒ Object
- #disabled? ⇒ Boolean
- #entity_posted? ⇒ Boolean
- #expected_tweet_time ⇒ Object
- #form_method ⇒ Object
- #form_path ⇒ Object
- #formatted_tweeted_at(default_msg = "not yet tweeted") ⇒ Object
- #manual_tweet! ⇒ Object
- #resend_tweet! ⇒ Object
- #tweet_style ⇒ Object
- #tweet_time ⇒ Object
- #tweeted? ⇒ Boolean
Class Method Details
.chronological ⇒ Object
178 179 180 |
# File 'app/models/tweet.rb', line 178 def chronological order "tweeted_at ASC" end |
.create_for(entity) ⇒ Object
157 158 159 |
# File 'app/models/tweet.rb', line 157 def create_for(entity) create (entity) end |
.create_tweet(params) ⇒ Object
141 142 143 144 145 |
# File 'app/models/tweet.rb', line 141 def create_tweet(params) raise "The entity_id is required!" if params[:entity_id].blank? raise "The entity_type is required!" if params[:entity_type].blank? create :entity_id => params[:entity_id].to_i, :entity_type => params[:entity_type], :tweet => params[:tweet] end |
.created_chronological ⇒ Object
186 187 188 |
# File 'app/models/tweet.rb', line 186 def created_chronological order "created_at ASC" end |
.created_reverse_chronological ⇒ Object
190 191 192 |
# File 'app/models/tweet.rb', line 190 def created_reverse_chronological order "created_at DESC" end |
.find_for(entity) ⇒ Object
165 166 167 168 |
# File 'app/models/tweet.rb', line 165 def find_for(entity) raise "Can only find tweets for entities!" unless entity.kind_of? Entity where({ :entity_id => entity.id, :entity_type => entity.entity_type }).first end |
.not_disabled ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'app/models/tweet.rb', line 194 def not_disabled result = all types = result.map(&:entity_type).uniq.select do |t| Setting[:"#{t}_tweet_style"] != :disabled end result.select { |x| types.include? x.entity_type } end |
.options_for(entity) ⇒ Object
161 162 163 |
# File 'app/models/tweet.rb', line 161 def (entity) { :entity_id => entity.id, :entity_type => entity.entity_type, :tweet => SimpleTemplate[Setting[:"#{entity.entity_type}_default_tweet"], :self => entity] } end |
.reverse_chronological ⇒ Object
182 183 184 |
# File 'app/models/tweet.rb', line 182 def reverse_chronological order "tweeted_at DESC" end |
.styles(entity) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'app/models/tweet.rb', line 204 def styles(entity) results = [] results << { :value => :disabled, :label => "tweet.style.disabled" } results << { :value => :manual, :label => "tweet.style.manual" } if Cartoonist::Entity[entity].include? Postable results << { :value => :automatic, :label => "tweet.style.automatic" } results << { :value => :automatic_timed, :label => "tweet.style.automatic_timed" } end results end |
.tweet_for(entity) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'app/models/tweet.rb', line 147 def tweet_for(entity) result = find_for entity if result result else new (entity) end end |
.tweeted ⇒ Object
170 171 172 |
# File 'app/models/tweet.rb', line 170 def tweeted where "tweeted_at IS NOT NULL" end |
.untweeted ⇒ Object
174 175 176 |
# File 'app/models/tweet.rb', line 174 def untweeted where "tweeted_at IS NULL" end |
.update_tweet(params) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'app/models/tweet.rb', line 132 def update_tweet(params) tweet = find params[:id].to_i return tweet if params[:resend_tweet].present? raise "Saving is not allowed!" unless tweet.allow_save? tweet.tweet = params[:tweet] tweet.save! tweet end |
Instance Method Details
#allow_resend_tweet? ⇒ Boolean
10 11 12 |
# File 'app/models/tweet.rb', line 10 def allow_resend_tweet? tweeted? && !disabled? && entity_posted? end |
#allow_save? ⇒ Boolean
22 23 24 |
# File 'app/models/tweet.rb', line 22 def allow_save? !tweeted? end |
#allow_tweet_now? ⇒ Boolean
6 7 8 |
# File 'app/models/tweet.rb', line 6 def allow_tweet_now? allow_save? && !disabled? && entity_posted? end |
#auto_tweet! ⇒ Object
49 50 51 52 53 54 |
# File 'app/models/tweet.rb', line 49 def auto_tweet! return if tweeted? return unless expected_tweet_time return unless Time.now >= expected_tweet_time send_tweet! end |
#disabled? ⇒ Boolean
26 27 28 |
# File 'app/models/tweet.rb', line 26 def disabled? tweet_style == :disabled end |
#entity_posted? ⇒ Boolean
14 15 16 17 18 19 20 |
# File 'app/models/tweet.rb', line 14 def entity_posted? if entity.kind_of? Postable posted = entity.posted? else posted = true end end |
#expected_tweet_time ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/tweet.rb', line 80 def expected_tweet_time case tweet_style when :disabled nil when :manual nil when :automatic entity.posted_at when :automatic_timed if tweet_time.blank? entity.posted_at elsif entity.posted_at parsed = Time.zone.parse("#{entity.posted_at.year}-#{entity.posted_at.month}-#{entity.posted_at.day} #{tweet_time.downcase}") result = Time.zone.local entity.posted_at.year, entity.posted_at.month, entity.posted_at.day, parsed.hour, parsed.min result = result + 1.day if result < entity.posted_at.to_time result end else raise "Invalid tweet style #{tweet_style}" end end |
#form_method ⇒ Object
72 73 74 75 76 77 78 |
# File 'app/models/tweet.rb', line 72 def form_method if new_record? :post else :put end end |
#form_path ⇒ Object
64 65 66 67 68 69 70 |
# File 'app/models/tweet.rb', line 64 def form_path if new_record? "/admin/tweets" else "/admin/tweets/#{id}" end end |
#formatted_tweeted_at(default_msg = "not yet tweeted") ⇒ Object
30 31 32 33 |
# File 'app/models/tweet.rb', line 30 def formatted_tweeted_at(default_msg = "not yet tweeted") return default_msg unless tweeted_at tweeted_at.localtime.strftime "%-m/%-d/%Y at %-l:%M %P" end |
#manual_tweet! ⇒ Object
35 36 37 38 39 40 |
# File 'app/models/tweet.rb', line 35 def manual_tweet! raise "Tweeting is not allowed!" unless allow_tweet_now? return if tweeted? return if disabled? send_tweet! end |
#resend_tweet! ⇒ Object
42 43 44 45 46 47 |
# File 'app/models/tweet.rb', line 42 def resend_tweet! raise "Tweeting is not allowed!" unless allow_resend_tweet? return unless tweeted? return if disabled? send_tweet! end |
#tweet_style ⇒ Object
56 57 58 |
# File 'app/models/tweet.rb', line 56 def tweet_style Setting[:"#{entity.entity_type}_tweet_style"] end |
#tweet_time ⇒ Object
60 61 62 |
# File 'app/models/tweet.rb', line 60 def tweet_time Setting[:"#{entity.entity_type}_tweet_time"] end |
#tweeted? ⇒ Boolean
102 103 104 |
# File 'app/models/tweet.rb', line 102 def tweeted? tweeted_at.present? end |